How do you detect key up / key down events from a

2020-08-26 04:37发布

问题:

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.