Search code examples
cocoamacosspotlightalt-tab

Catch system event as Cmd-Tab or Spotlight in a Cocoa App


In a Cocoa App, I'm trying to find a way to catch system events like the app switcher usually launched with Cmd-Tab or spotlight, usually launched by Cmd-Space. I'm looking for either a way to catch the key event or any another way that would tell me that one of those event is about to happen, and ideally cancel it.

Apple Screen Sharing remote desktop app does it, so it should be possible. It catches those events and send them to the connected remote computer.

Here is what I already tried :

  • Catching events with the sendEvent method in NSApplication. I see all events like the Cmd keydown, the Tab keydown, but when both are pressed, I see nothing.
  • Registering a Carbon Hot key listener. I can register anything like Cmd+Q, but again, when I register Cmd+Tab, it does not respond.

Any other ideas ?


Solution

  • Found it! In my WindowViewController.m file

    #import <Carbon/Carbon.h>
    
    void *oldHotKeyMode;
    
    - (void)windowDidBecomeKey:(NSNotification *)notification{
        oldHotKeyMode = PushSymbolicHotKeyMode(kHIHotKeyModeAllDisabled);
    }
    - (void)windowDidResignKey:(NSNotification *)notification{
        PopSymbolicHotKeyMode(oldHotKeyMode);
    }
    

    This is pretty magic ! and it passes the new Apple sandboxing requirement for the Mac App Store !