Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one detect Mission Control or Command-Tab switcher superseding one's program in OS X?

I'm trying to use CGAssociateMouseAndMouseCursorPosition(NO) in a program. This disconnects the mouse from the on screen cursor when your application is "in the foreground". Unfortunately it also disconnects it when Mission Control or the application switcher or who knows what else comes up.

So far I know:

  • The application is still active.
  • The window is still key.
  • Nothing is sent to the default notification center when these things come up.
  • The application stops receiving mouse moved events, but an NSEvent addGlobalMonitorForEventsMatchingMask:handler: also does not receive them, which is strange to say the least. It should receive any events not delivered to my application. (I was planning to detect the missing events to know when to associate the mouse again.

So, is there a way to detect when my application is no longer in control, specifically because Mission Control or the switch has taken over? They really expect the mouse to work and I need to restore that association for them.

like image 726
bitmusher Avatar asked Feb 19 '12 20:02

bitmusher


People also ask

How do you get to Mission Control on a Mac?

Open Mission ControlSwipe up with three or four fingers on your trackpad, or double-tap the surface of your Magic Mouse with two fingers. Open the Mission Control app, which is in your Applications folder. Press the Mission Control key on your Apple keyboard or Touch Bar.

How does Command Tab work on Mac?

To switch between open apps, press Command-Tab. Release the keys when the app you want is selected. To show all the open windows in the current app, Control-click its icon in the Dock, then choose Show All Windows. To browse other app windows, press Tab.

How do you change Mission Control on a Mac?

On your Mac, use Mission Control System Preferences to change options and shortcuts for showing and hiding app windows and the desktop. Learn how to use Mission Control. To change these preferences, choose Apple menu > System Preferences, then click Mission Control .


1 Answers

I share your surprise that a global event monitor isn't seeing the events. In a similar situation, I used a Quartz Event Tap for a similar purpose. The Cocoa global event monitor is quite similar to event taps, so I figured it would work.

I put the tap on kCGAnnotatedSessionEventTap and compared the result from CGEventGetIntegerValueField(event, kCGEventTargetUnixProcessID) to getpid() to determine when the events were going to another app (e.g. Mission Control or Exposé). (I disable the tab when my app resigns active status, so it should only receive events destined for another app when this sort of overlay UI is presented.)

By the way, you mentioned monitoring the default notification center, but, if there's a notification about Mission Control or the like, it's more likely to come to the distributed notification center (NSDistributedNotificationCenter). So, it's worth checking that.

like image 167
Ken Thomases Avatar answered Oct 19 '22 11:10

Ken Thomases