I'd like to add hardware keyboard support to my app so users may trigger a function to be called by pressing a certain key on the keyboard at any time. I found this article and was able to make it work great in Objective-C. I've converted it to Swift, but for some reason the keyPressed
method is not being called after I press "c". I confirmed keyCommands
is called as soon as the user presses any key on the keyboard. I'm testing with the iOS Simulator and the Mac's keyboard.
What is the problem here in my Swift code?
override func canBecomeFirstResponder() -> Bool {
return true
}
func keyCommands() -> [AnyObject]? {
var keyCommands = []
struct Static {
static var onceToken : dispatch_once_t = 0
}
dispatch_once(&Static.onceToken) {
let command = UIKeyCommand(input: "c", modifierFlags: nil, action: "keyPressed:")
keyCommands = [command]
}
return keyCommands
}
func keyPressed(command: UIKeyCommand) {
println("user pressed c") //never gets called
}