I have been searching for the past through days, trying to figure out how and if it would be possible to listen to keydown events while in the background. For example when using the program "Ventrilo" or "Teamspeak" it allows you to talk to your video game team, while playing a video game. Therefore these two programs listen for the key when they are in the background. My question is how is this done? Some examples, tutorials would be awesome! I'm also still kinda new at this, so please be descriptive.
The only way I know in cocoa is using a global event monitor. If you call the method below, the app will listen for a control-alt-/ and then put up an alert. A possible problem with this approach is that the block will run (but not present the alert) every time you press a key, if you are doing something very processor intensive, this might slow it down a bit. I think there is a way to do it in Carbon that doesn't have this problem, but I don't know enough about Carbon to post about that.
+(void)listenForKey {
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event)
{
NSUInteger key = 44; // 44 is forward slash
NSUInteger modifier = NSControlKeyMask | NSAlternateKeyMask;
if ([event keyCode] == key && [NSEvent modifierFlags] == modifier)
NSRunCriticalAlertPanel(nil,@"Control-alt / was detected",nil,nil,nil);
}];
}