Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercepting NSMenu key events

I am making a search field, which opens a NSMenu similar to Finder and Mail

The only problem, which drives me crazy, is that i cannot intercept the key events, once the menu is open

  • I even subclassed the NSApplication sendEvent, but was surprised to find out, that during menu, there is no call to Applications sendAction:

  • I subclassed performKeyEquivalent: NSMenu but it is not called

 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent
  • I am listening for the delegate call on menuHasKeyEquivalent: but it doesn't get called
 - (BOOL)menuHasKeyEquivalent:(NSMenu*)menu forEvent:(NSEvent*)event target:(id*)target action:(SEL*)action

enter image description here

How to intercept the keyboard input during NSMenu

like image 697
Peter Lapisu Avatar asked Aug 12 '15 15:08

Peter Lapisu


1 Answers

It is possible with a usage of Carbon event handlers. These functions allow interception of keyboard events while menu is open: GetEventDispatcherTarget() and InstallEventHandler(). This is a hack, but in fact, it works reliably across all MacOSX versions starting from 10.7. Here's the source code that could be used as a reference implementation: https://github.com/mikekazakov/MGKMenuWithFilter

like image 141
Migun Avatar answered Nov 03 '22 00:11

Migun