Respond to Keyboard Presses
This post is brought to you by Sentry, mobile monitoring considered "not bad" by 4 million developers.
let concatenatedThoughts = """
Welcome to Snips! Here, you'll find a short, fully code complete sample with two parts. The first is the entire code sample which you can copy and paste right into Xcode. The second is a step by step explanation. Enjoy!
"""
The Scenario
Add support for keyboard presses in your app.
import UIKit
class KeyboardViewController: UIViewController {
    
}
// MARK: Keyboard Shortcuts
extension KeyboardViewController {
	// 1
    override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
        super.pressesBegan(presses, with: event)
        // 2
        presses.first?.key.flatMap{ onKeyPressed($0) }
    }
    
    // 3
    func onKeyPressed(_ key: UIKey) {
        switch key.keyCode {
        case .keyboardSpacebar:
            print("On spacebar pressed")
        case .keyboardN where key.modifierFlags.contains(.command):
            print("On N + Command pressed")
        default:
            break
        }
    }
}
The Breakdown
Step 1
Override touch handling events to respond to keyboard presses such as pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?). In addition, you can also respond to its similar variants such as pressesCancelled or pressesEnded.
Step 2
Since the set of UIPress objects will hold an instance of UIKey?, we flatMap out any nil instance since the touch event is not exclusive to keyboard presses.
Step 3
Finally, we can switch on the UIKey’s keyCode property to respond to the ones we are interested in. Further, you can tack on a where clause to handle complex key press combinations.
Unlike adding keyboard shortcuts, this technique is great for adhoc responses to the keyboard where it wouldn’t make sense to register a UIKeyCommand for the things you’re trying to do (i.e. move a character in a video game by using the W,A,S and D keys.).
Until next time ✌️