Search code examples
macoscocoakeyboard-shortcutsmenubarnsmenuitem

Keyboard shortcuts not working in Menubar Application for macOS


I created a macOS Application which only runs in the Menubar.

My Menubar Application

In Xcode I added a Shortcut for some of the NSMenuItem with the Key Equivalent Property in the Inspector.

In this Case it is Shift+F4 and Shift+F5 Xcode Screenshot

The Shortcuts do work, but only, when the menu is opened. I guess this is because, the application is in the background for the rest of the time.

How can I get the shortcuts work, even when the menu is not open?

The Allowed When Hidden-Checkbox won't work. I assume it refers to the item itself, which can be marked as hidden.

Any ideas and suggestions will be greatly appreciated.


Solution

  • It looks like you are using NSStatusItem.

    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.

    I'm answer this from another post. Please refer that post for more detail.