Raise a system event as Cmd-Tab or Spotlight in Cocoa application

In a Cocoa application, I am trying to find a way to catch system events, such as an application switch, usually triggered by Cmd-Tab or spotlight, usually triggered by Cmd-Space. I’m looking for either a way to catch a key event, or any other way that tells me that one of these events should happen, and ideally cancel it.

Apple Screen Sharing for Remote Desktop does this, so it should be possible. It catches these events and sends them to the connected remote computer.

Here is what I have already tried:

  • Search for events using the sendEvent method in NSApplication. I see all events, such as Cmd keydown, Key keydown, but when both are pressed, I see nothing.
  • Register a Carbon hotkey listener. I can register something like Cmd + Q, but then again, when I register Cmd + Tab, it does not respond.

Any other ideas?

+4
source share
3 answers

Found! 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 conveys Apple's new sandbox requirement for the Mac App Store!

+1
source
+2
source

I will tell you how to catch cmd + tab. But keep in mind that it will only work in full screen mode. I believe that in windowed mode this is not possible. The code is pretty simple. This is a small fix for the SDL mac code - an update to handle the cmd + tab in full screen.

 NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ]; if ( event == nil ) { break; } if (([event type] == NSKeyDown) && ([event modifierFlags] & NSCommandKeyMask) &&([[event characters] characterAtIndex:0] == '\t') { do something here } 
+1
source