11

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?

James Bedford
  • 28,702
  • 8
  • 57
  • 64
mseddon
  • 1,815
  • 17
  • 28
  • Are you asking about framework capabilities, App Store approval, developer sentiment, or what? – Justin Feb 28 '14 at 00:02
  • 1
    If you need a definitive answer from Apple, you could build a sample project and raise a TSI: https://developer.apple.com/support/technical/submit/ – Robotic Cat Feb 28 '14 at 00:02
  • Removed vitriol in response to @user2864740, I am not looking for a holy war, merely a method of detecting basic keyboard input *in a way that is viable for commercial deployment*. – mseddon Feb 28 '14 at 00:07
  • 1
    Good edit. I've retracted my close vote. – Justin Feb 28 '14 at 00:09
  • @RoboticCat, as a newbie to iOS development, I appreciate this pointer, and may wind up taking this route, though from a legalese standpoint it would be easy to discard my code as merely 'undocumented'. I hope that any answer Apple give would be definitive, and useful for other people who stumble across this problem, (those porting QT, GTK, SDL or similar toolkits) – mseddon Feb 28 '14 at 00:15
  • @Justin app store approval- I am porting an in-house toolkit to iOS, but I want to be able to look a client in the eye and say "yes, your app won't randomly disappear because I did something naughty that Apple didn't like." There is, afaict, no guidance in this area. – mseddon Feb 28 '14 at 00:26
  • 1
    I don't believe this is currently possible, which is ridiculous. See also my comment in https://stackoverflow.com/questions/19235762/how-can-i-support-the-up-and-down-arrow-keys-with-a-bluetooth-keyboard-under-ios . – James Bedford May 27 '18 at 19:22
  • 1
    This is a good question and I'm looking for an answer too. Detecting key up / key down state if very important if you want to develop an action game on iOS with bluetooth keyboard support. For example, press on arrow keys to manoeuvre a spaceship or press down Enter key to fire. No game players want to play the game by tediously click (press and release) those keys. – Hongkun Wang Oct 02 '19 at 15:34
  • This is an excellent use case that I hadn't considered. – mseddon Oct 03 '19 at 19:48

1 Answers1

8

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)
    }
}
Benjamin Kindle
  • 1,736
  • 12
  • 23