Created
March 26, 2014 04:02
-
-
Save keefo/9776789 to your computer and use it in GitHub Desktop.
register a global hotkey in Cococa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Carbon/Carbon.h> | |
@interface LXAppDelegate() | |
{ | |
EventHotKeyRef global_hotkey_ref; | |
} | |
- (void)hotKeyAction; | |
@end | |
static pascal OSStatus HotKeyEventHandlerProc(EventHandlerCallRef nextHandler,EventRef theEvent, void *userData) | |
{ | |
EventHotKeyID hotKeyID; | |
OSStatus err = eventNotHandledErr; | |
UInt32 eventClass = GetEventClass( theEvent ); | |
UInt32 eventKind = GetEventKind( theEvent ); | |
switch ( eventClass ) | |
{ | |
case kEventClassKeyboard: | |
if ( eventKind == kEventHotKeyReleased ){ | |
GetEventParameter( theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(EventHotKeyID), NULL, &hotKeyID ); | |
if ( hotKeyID.signature == 'myglobalkey' ){ | |
switch ( hotKeyID.id ){ | |
case 1:{ | |
LXAppDelegate *appdelegate = (__bridge LXAppDelegate *)userData; | |
[appdelegate hotKeyAction]; | |
} | |
break; | |
} | |
} | |
} | |
break; | |
} | |
return err; | |
} | |
@implementation LXAppDelegate | |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification | |
{ | |
// Insert code here to initialize your application | |
EventTypeSpec eventType; | |
eventType.eventClass = kEventClassKeyboard; | |
eventType.eventKind = kEventHotKeyReleased; | |
InstallApplicationEventHandler(NewEventHandlerUPP(HotKeyEventHandlerProc), 1, &eventType, (__bridge void*)self, NULL); | |
EventHotKeyID keyID = { 'myglobalkey', 1 }; | |
RegisterEventHotKey(kVK_ANSI_K, cmdKey, keyID, GetApplicationEventTarget(), 0, &global_hotkey_ref); | |
} | |
- (void)hotKeyAction | |
{ | |
NSLog(@"cmd+k hotKeyAction"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment