Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andyhullinger/10836818351f958bbc654ce42063732b to your computer and use it in GitHub Desktop.
Save andyhullinger/10836818351f958bbc654ce42063732b to your computer and use it in GitHub Desktop.
var keys = [UIKeyCommand]()
override func viewDidLoad() {
super.viewDidLoad()
//configureGameControllers()
for digit in "abcdefghijklmnopqrstuvwxyz"
{
keys.append(UIKeyCommand(input: String(digit), modifierFlags: nil, action: Selector("keyPressed:")))
}
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override var keyCommands: [AnyObject]? {
get {
return keys
}
}
func keyPressed(command: UIKeyCommand) {
println("user pressed \(command.input)")
}
@andyhullinger
Copy link
Author

#17: UIKeyCommand

Tuesday June 16th, 2015
iOS has had support for hardware keyboard shortcuts for a while now. New in iOS 9, Apple has made some great improvements to how apps can take advantage of them. Your app can now register keyboard shortcuts per view controller, and all currently valid shortcuts will be neatly
summarized when the user brings up the new keyboard shortcuts view.
Wiring up a new keyboard shortcut in this system couldn't be easier. Just define a new function, create a new UIKeyCommand and then call addKeyCommand on your view controller:
// inside some UIViewController subclass:

override func viewDidLoad() {
super.viewDidLoad()

let shortcut = UIKeyCommand(
input: "n",
modifierFlags: UIKeyModifierFlags.Command,
action: "createNewFile:"
)

addKeyCommand(shortcut)
}

func createNewFile(command: UIKeyCommand) {
// do the thing
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment