swift - Cocoa Listen to Keyboard command+up Event -
i working on macos app , handle local hotkey event (command + arrow key) in nsviewcontroller
.
here's how swift:
override func keydown(with event: nsevent) { let modifierkeys = event.modifierflags.intersection(.deviceindependentflagsmask); let hascommand = modifierkeys == .command; switch int(event.keycode) { case kvk_uparrow hascommand: print("command up"); break; case kvk_ansi_b hascommand: print("command b"); break; default: break; } }
when build , press command+up in view, console shows nothing. when press command+b, "command b" logged out.
so why isn't working command+up? how should achieve this?
i've found solution:
self.keymonitor = nsevent.addlocalmonitorforevents(matching: nseventmask.keydown, handler: { (event) -> nsevent? in if (event.modifierflags.contains(.command)){ if (int(event.keycode) == kvk_uparrow){ print("command up"); return nil; } } return event; });
the key point interrupt keydown event , prevent being dispatched returning nil
Comments
Post a Comment