Skip to content

Instantly share code, notes, and snippets.

@chapados
Forked from isaac/gist:114372
Created May 20, 2009 00:14
Show Gist options
  • Save chapados/114521 to your computer and use it in GitHub Desktop.
Save chapados/114521 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