Skip to content

Instantly share code, notes, and snippets.

@taxilian
Created June 22, 2011 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save taxilian/1039601 to your computer and use it in GitHub Desktop.
Save taxilian/1039601 to your computer and use it in GitHub Desktop.
partial example of global keyboard event handler
void *EventLoop(void *args)
{
int state;
pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, &state);
pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &state);
// In one thread run the event loop to get hotkey presses
EventHotKeyID gMyHotKeyID1, gMyHotKeyID2;
EventTypeSpec eventType;
/* Register event handler for hotkeys */
eventType.eventClass=kEventClassKeyboard;
eventType.eventKind=kEventHotKeyPressed;
InstallApplicationEventHandler(NewEventHandlerUPP(MyHotKeyHandler),1,&eventType,NULL,NULL);
/* Register the hotkeys */
/* Regular F1-F12=F(fkey)*/
gMyHotKeyID1.signature='gc01';
gMyHotKeyID1.id=1;
RegisterEventHotKey(fnkeyTable[Typer::getInstance()->getFKey()], 0, gMyHotKeyID1, GetApplicationEventTarget(), 0, &gMyHotKeyRef1);
/* Also register for shift F8 -- 0x64 == F8, 56 == Shift */
gMyHotKeyID2.signature='gc02';
gMyHotKeyID2.id=2;
RegisterEventHotKey(fnkeyTable[Typer::getInstance()->getFKey()], shiftKey, gMyHotKeyID2, GetApplicationEventTarget(), 0, &gMyHotKeyRef2);
// Run the event loop
EventTargetRef target = GetEventDispatcherTarget();
while(1) {
EventRef event;
if (ReceiveNextEvent(0, NULL, (kEventDurationSecond / 10), true, &event) == noErr) {
SendEventToEventTarget (event, target);
ReleaseEvent(event);
}
pthread_testcancel();
}
// RunApplicationEventLoop();
pthread_exit(0);
}
@Noitidart
Copy link

Hi there does this work from a thread? Becase I am trying to just do InstallApplicationEventHandler and RegisterEventHotKey from a spawned thread but it keeps crashing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment