Created
August 26, 2016 01:15
-
-
Save andyhullinger/10836818351f958bbc654ce42063732b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#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
}