since this is for tutorial purposes assume that all of this is in main.m (or .c) , also this will only compile properly under an i386 architecture because carbon event loops aren't support by 64 bit archs yet...go figure.
Post a comment with any questions
// *This code is meant to be for tutorial purposes*
#import <Carbon/Carbon.h>
//our callback for the event this is called every time 'your' event happens
OSStatus keyHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData){
printf("Hey! I trapped a global key event\n");
return noErr;
}
//register interest in the events you would like to receive notifications about
void setupEventHandler(){
EventHotKeyRef hotKeyRef;
EventHotKeyID hotKeyID;
EventTypeSpec eventType;
eventType.eventClass = kEventClassKeyboard;
eventType.eventKind = kEventHotKeyPressed;
InstallEventHandler(GetApplicationEventTarget(),
&keyHandler,
1,
&eventType,
NULL,
NULL);
hotKeyID.signature = '1';
hotKeyID.id = 1;
RegisterEventHotKey(123,
optionKey,
hotKeyID,
GetApplicationEventTarget(),
0,
&hotKeyRef);
}
int main (int argc, const char * argv[]) {
setupEventHandler();
RunApplicationEventLoop();
return 0;
}
Still, seems pretty useless. The only time I can think you'd need this is if you were writing a low-level exception handler script in CalaBaza. Can you explain the "gui" part though?
ReplyDeleteGUI = Graphical User Interface, the usual way of trapping events is by having an 'event capturing method' in a class thats gui object has first responder rights. This is a way to write a global key trapper without using a gui
ReplyDelete