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?

9
  • Are you asking about framework capabilities, App Store approval, developer sentiment, or what? Commented Feb 28, 2014 at 0:02
  • 1
    If you need a definitive answer from Apple, you could build a sample project and raise a TSI: developer.apple.com/support/technical/submit Commented Feb 28, 2014 at 0:02
  • 1
    Good edit. I've retracted my close vote. Commented Feb 28, 2014 at 0:09
  • 1
    I don't believe this is currently possible, which is ridiculous. See also my comment in stackoverflow.com/questions/19235762/… . Commented May 27, 2018 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. Commented Oct 2, 2019 at 15:34

1 Answer 1

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) } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Do I need to add a check for older iOS versions or is this handled by iOS SDK?
@MrStahlfelge I just tested with iOS 13.2 and it worked without anything special. lower iOS versions just never include UIKey values in the UIPress objects.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.