3

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?

k.c
  • 35
  • 3

2 Answers2

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.

Community
  • 1
  • 1
ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
  • I am sorry, let me clarify, if it makes any difference. My application is only a NSStatusItem, there is no other main menu or window. If this is the case, how can I set it to be in focus. – k.c Apr 26 '11 at 02:39
  • You can't, but that doesn't matter. The status bar is a different application which is only in focus when you have the menu open. I posted this incomplete while finding a link the the actual solution, but I found it now so I will add it to the post. – ughoavgfhw Apr 26 '11 at 02:44
  • Thanks, I had not set the key equivalent for the main menu, just for the NSStatusItem. – k.c Apr 30 '11 at 00:35
-1

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.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498