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