How do you detect key up / key down events from a hardware keyboard on iOS?

╄→гoц情女王★ 提交于 2020-07-18 09:40:49

问题


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.

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


回答1:


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)
    }
}



回答2:


Brace yourself. The answer is- this is not possible.

Yes. It is 2020, 6 years after I initially posted this question, and the answer is still- while iOS internally handles keyup/keydown messages, it is just not legal to intercept them.

What this says for the state of Apple's interest in iOS as a realistic computing environment, I don't know, but until someone posts a better answer, the fact is, you are supposed to accept keyboard handling as a purely input method impulse. Apparently touch won so conclusively, you will never again be able to recreate the ability to detect a sustained keypress. Nice.



来源:https://stackoverflow.com/questions/22083400/how-do-you-detect-key-up-key-down-events-from-a-hardware-keyboard-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!