macOS - Menu actions do not trigger any action if the app is not active

232 views Asked by At

I am building this app that will sit on the menu bar.

So, as normally is done with this kind of app, you adjust the statusItem menu to be the menu of your app.

The items on that menu trigger actions on the first responder, that in my case is the viewController of that app.

This is the problem: this works if the app is active but if the app is not active or hidden, this fails. I guess the actions triggered by the menu items will fire to the responder chain but there is no one listening, because the app is hidden or not active.

How do I do in that case? Should I create a singleton and add that singleton to the responder chain? I say singleton because that would be always loaded in memory. Does this singleton have to be a subclass of some special class?

How to do if actions from this menu should trigger stuff in the viewController?

2

There are 2 answers

0
Ivan Ičin On BEST ANSWER

You should be able to handle first responder in the AppDelegate, which should work if the app is not active.

0
Chris Zielinski On

See NSEvent.addGlobalMonitorForEvents(matching:handler:) & note the discussion:

Key-related events may only be monitored if accessibility is enabled or if your application is trusted for accessibility access (see AXIsProcessTrusted()).