Created
June 22, 2011 06:36
-
-
Save taxilian/1039601 to your computer and use it in GitHub Desktop.
partial example of global keyboard event handler
This file contains hidden or 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
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there does this work from a thread? Becase I am trying to just do
InstallApplicationEventHandler
andRegisterEventHotKey
from a spawned thread but it keeps crashing.