问题描述
我正在搜索一个字段,它会打开一个类似于Finder和Mail的NSMenu
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
-
我什至将NSApplication sendEvent子类化,但惊讶地发现,在菜单执行期间,没有调用Applications sendAction:
I even subclassed the NSApplication sendEvent, but was surprised to find out, that during menu, there is no call to Applications sendAction:
我将performKeyEquivalent子类化为NSMenu,但未将其命名为
I subclassed performKeyEquivalent: NSMenu but it is not called
- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
- 我正在侦听menuHasKeyEquivalent上的委托调用:但没有被调用
- (BOOL)menuHasKeyEquivalent:(NSMenu*)menu forEvent:(NSEvent*)event target:(id*)target action:(SEL*)action
如何在NSMenu期间拦截键盘输入
How to intercept the keyboard input during NSMenu
推荐答案
KeyEquivalent
用于不拦截所有键的菜单快捷方式.无需拦截NSMenu
事件,并且无法将其用于此目的.要创建一个建议框,您基本上需要创建一个自定义NSWindow
并使它看起来像菜单.
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提供了示例代码,他们解释了有时它会在 WWDC 2010会议145 中出现.
Apple provides a Sample Code and they explained it in WWDC 2010 session 145 at some point.
这篇关于拦截NSMenu键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!