Intercepting NSMenu key events
Asked Answered
P

2

6

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

Preceding answered 12/8, 2015 at 15:3 Comment(3)
KeyEquivalent is for menu shortcuts not intercepting all the keys, and only works on application's menu bar. I believe making a custom window instead of NSMenu would be a better approach. Also take a look at this question: #6070992Dedra
Peter, can you share with us your solution?Runesmith
I did up not using NSMenu as i found several problems with this approach... for now i didnt found time to make this into reusable code that can be shared, maybe i will come back to itPreceding
D
0

KeyEquivalent is for menu shortcuts not intercepting all the keys. There is no need to intercept the NSMenu events and it is not possible to be used for that purpose. To make a suggestion box you basically need to make a custom NSWindow and make it look like a menu.

Apple provides a Sample Code and they explained it in WWDC 2010 session 145 at some point.

Dedra answered 17/8, 2015 at 8:5 Comment(0)
L
5

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

Lowland answered 18/5, 2017 at 6:36 Comment(0)
D
0

KeyEquivalent is for menu shortcuts not intercepting all the keys. There is no need to intercept the NSMenu events and it is not possible to be used for that purpose. To make a suggestion box you basically need to make a custom NSWindow and make it look like a menu.

Apple provides a Sample Code and they explained it in WWDC 2010 session 145 at some point.

Dedra answered 17/8, 2015 at 8:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.