ios - Listening for hardware keyboard key presses in Swift -


i'd add hardware keyboard support app users may trigger function called pressing key on keyboard @ time. found this article , able make work great in objective-c. i've converted swift, reason keypressed method not being called after press "c". confirmed keycommands called user presses key on keyboard. i'm testing ios simulator , mac's keyboard.

what problem here in 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 } 

the problem keycommands called every time key pressed , every time initialize array empty, return it. first time method called return correct array, second time return array no content.

to fix issue, decided store key commands array property, in method check if nil, , if create it, otherwise return stored property.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -