菜单中的可可键等效项不起作用
我创建了一个 NSStatusItem,并在界面生成器(Xcode 4.0)中分配了两个关键等效项。这些用于首选项菜单 cmd- 和退出选项 cmd-q。当菜单突出显示/打开时,这两者都将起作用,但即使应用程序位于最前面,也将不起作用。关于为什么会发生这种情况或者我如何改变这种情况有什么想法吗?
I have created a NSStatusItem and I have assigned two key equivalents within interface builder (Xcode 4.0). These are for the preference menu cmd-, and the quit option cmd-q. Both of these will work when the Menu is highlighted/open but will not work otherwise even if the application is the foremost. Any ideas on why this is happening or how I can change this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
操作系统将其不处理的按键事件传递给前端应用程序,前端应用程序将它们与主菜单和当前窗口中的任何按键等效项进行比较。它们不会与状态栏中的项目进行比较,并且由于状态栏实际上是不同的应用程序,因此您的应用程序位于前台并不重要。当您打开菜单时,它就是焦点窗口。这就是它在这些情况下起作用的原因。
由于状态项仅在菜单打开时才处于活动状态,因此您需要使用其他两种方法之一来获取等效键。第一种较新的方法是使用 事件点击。我从未使用过这个,所以我无法建议如何设置它。另一种较旧的选项是使用热键。此方法将来可能会不受支持,但如果您感兴趣,我在 这个答案。
The operating system passes key events that it doesn't handle to the front application, which compares them to any key equivalents in its main menu and current window. They are not compared to items in the status bar, and since the status bar is actually a different application, it won't matter that your application is in the foreground. When you have the menu open, it is the window in focus. This is why it works in those circumstances.
Since the status item is only active while the menu is open, you need to get the key equivalents using one of two other methods. The first, newer method is to use event taps. I have never used this, so I can't advise on how to set it up. The other, older option, is to use hot keys. This method may become unsupported in the future, but if you are interested I posted an example in this answer.
我的猜测:您的
NSMenu
不在响应者链中。如果您能弄清楚如何将其放入其中,则可能可以使其发挥作用。My guess: your
NSMenu
is not in the responder chain. If you can figure out how to get it in there, you can probably get this to work.