如何在iOS上通过硬件键盘检测按键上/下键事件? [英] How do you detect key up / key down events from a hardware keyboard on iOS?

查看:95
本文介绍了如何在iOS上通过硬件键盘检测按键上/下键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管在SO上发布了许多有关如何使键盘工作的方法,例如接收iPhone键盘事件,没有记录.

While there are many methods posted on SO regarding hacking keyboards to work, for example, How can I support the up and down arrow keys with a Bluetooth keyboard under iOS 7, or Receive iPhone keyboard events, none of them are documented.

是否可以使用公共API从iOS中的硬件键盘(例如蓝牙)检测到keyUp:/keyDown:输入事件?

Is it possible to detect a keyUp: / keyDown: input event from a hardware keyboard (e.g. bluetooth) in iOS using public APIs?

推荐答案

从iOS 13.4开始,由于引入了 pressesBegan 事件发生. Swift By Sundell的本指南涵盖了一些示例.这是相关的位:

As of iOS 13.4, this is now possible due to the introduction of UIKey, which is included on pressesBegan events now when a keyboard key is pressed. this guide from Swift By Sundell covers some examples. Here is the relevant bit:

class EditorViewController: UIViewController {
    ...

    override func pressesBegan(_ presses: Set<UIPress>,
                               with event: UIPressesEvent?) {
        super.pressesBegan(presses, with: event)
        presses.first?.key.map(keyPressed)
    }

    override func pressesEnded(_ presses: Set<UIPress>,
                               with event: UIPressesEvent?) {
        super.pressesEnded(presses, with: event)
        presses.first?.key.map(keyReleased)
    }

    override func pressesCancelled(_ presses: Set<UIPress>,
                                   with event: UIPressesEvent?) {
        super.pressesCancelled(presses, with: event)
        presses.first?.key.map(keyReleased)
    }
}

这篇关于如何在iOS上通过硬件键盘检测按键上/下键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆