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

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -