My IOS
app need to handle a connected bluetooth keyboard to do some function in the app, I tried this method, but the xcode cannot found the Cocoa
Then I tried this method, this time xcode cannot find the canBecomeFirstResponder
in super class.
How can I capture keyboard event in my app?
import UIKit
class ViewController: UIViewController {
var keys = [UIKeyCommand]()
override func viewDidLoad() {
super.viewDidLoad()
for digit in "1234567890abcdefghijklmnopqrstuvwxyz"
{
keys.append(UIKeyCommand(input: String(digit), modifierFlags: [], action: Selector(("keyPressed:"))))
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override var keyCommands: [AnyObject]? {
get {
return keys
}
}
func keyPressed(command: UIKeyCommand) {
print("user pressed \(command.input)")
}
}