-1

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?

Duck
  • 34,902
  • 47
  • 248
  • 470
  • Is the action not responding at all, or is the action actually being called, but trying to do something with a window that isn't there? – red_menace Sep 05 '19 at 18:09
  • The actions connected to the menuitems are being called if the app is active but if the app is behind others and another one is active, the actions do nothing. I mean, I chose items on the app's menu that is sitting on the menubar and nothing happens. By menubar I mean when the app is near the clock. – Duck Sep 05 '19 at 20:08
  • I have "resolved" this by forcing the app to be active when the menu is selected but this is not good for several reasons. First because it will require two clicks, one to bring the app active and another one to trigger an action when a menu item is selected. I see other apps sitting on the menu bar that do things with one click. – Duck Sep 05 '19 at 20:12
  • You shouldn't need two clicks, just have the menu action(s) bring your app's window to the front or disable items that need a window until the app is activated. – red_menace Sep 05 '19 at 21:37
  • 1
    It's a little strange that your menu hooks up to a view controller. View controllers are meant to (you know) control views, but you want this to work when there is no view active. Shouldn't you have connected this to the app delegate instead? – Ted Wrigley Sep 05 '19 at 22:48

2 Answers2

0

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

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
0

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()).

Chris Zielinski
  • 1,231
  • 1
  • 9
  • 11