Skip to content

Instantly share code, notes, and snippets.

@glejeune
Forked from chapados/shortcut.m
Created March 12, 2010 18:45
Show Gist options
  • Save glejeune/330610 to your computer and use it in GitHub Desktop.
Save glejeune/330610 to your computer and use it in GitHub Desktop.
// shortcut.m
//
// compile:
// gcc shortcut.m -o shortcut.bundle -g -framework Foundation -framework Carbon -dynamiclib -fobjc-gc -arch i386 -arch x86_64
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
@interface Shortcut : NSObject
{
id delegate;
}
@property (assign) id delegate;
- (void) addShortcut;
- (void) hotkeyWasPressed;
@end
OSStatus myHotKeyHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData);
@implementation Shortcut
@synthesize delegate;
OSStatus myHotKeyHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData)
{
NSLog(@"YEAY WE DID A GLOBAL HOTKEY");
if ( userData != NULL ) {
id delegate = (id)userData;
if ( delegate && [delegate respondsToSelector:@selector(hotkeyWasPressed)] ) {
[delegate hotkeyWasPressed];
}
}
return noErr;
}
- (void) addShortcut
{
EventHotKeyRef myHotKeyRef;
EventHotKeyID myHotKeyID;
EventTypeSpec eventType;
eventType.eventClass=kEventClassKeyboard;
eventType.eventKind=kEventHotKeyPressed;
if ( delegate == nil )
delegate = self;
EventTargetRef eventTarget = (EventTargetRef) GetEventMonitorTarget();
InstallEventHandler(eventTarget, &myHotKeyHandler, 1, &eventType, (void *)delegate, NULL);
myHotKeyID.signature='mhk1';
myHotKeyID.id=1;
RegisterEventHotKey(49, controlKey+optionKey, myHotKeyID, eventTarget, 0, &myHotKeyRef);
}
- (void) hotkeyWasPressed {};
@end
void Init_shortcut(void) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment