问题
I followed this awesome tutorial to set up a hotkey - http://dbachrach.com/blog/2005/11/program-global-hotkeys-in-cocoa-easily/
However I am doing this from a thread. So I suspect I need to run some kind of event loop. I have tried:
WaitNextEvent- with theeveryEventargument set to0- I guessed on that one because I can't figure out where they define it here - https://developer.apple.com/legacy/library/documentation/AppleScript/Conceptual/AppleEvents/dispatch_aes_aepg/dispatch_aes_aepg.html#//apple_ref/doc/uid/TP40001449-CH204gotEvent = WaitNextEvent(everyEvent, &event, 32767L, cursorRgn);RunCurrentEventLoop- https://developer.apple.com/legacy/library/documentation/Carbon/Reference/Carbon_Event_Manager_Ref/index.html#//apple_ref/c/func/RunCurrentEventLoopCFRunLoopRun- https://developer.apple.com/library/ios/documentation/CoreFoundation/Reference/CFRunLoopRef/index.html#//apple_ref/c/func/CFRunLoopRunCFRunLoopInMode- https://developer.apple.com/library/ios/documentation/CoreFoundation/Reference/CFRunLoopRef/index.html#//apple_ref/c/func/CFRunLoopRunInMode
But I can't seem to get the hotkey to work. This is my code:
//Register the Hotkeys
EventHotKeyRef gMyHotKeyRef;
EventHotKeyID gMyHotKeyID;
gMyHotKeyID.signature='htk1';
gMyHotKeyID.id=1;
EventTypeSpec eventType;
eventType.eventClass=kEventClassKeyboard;
eventType.eventKind=kEventHotKeyPressed;
InstallApplicationEventHandler(&MyHotKeyHandler,1,&eventType,NULL,NULL);
RegisterEventHotKey(49, shiftKey, gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef);
Can you please advise.
I also tried InstallEventHandler with GetEventDispatcherTarget instead of GetEventApplicationTarget.
Thanks
来源:https://stackoverflow.com/questions/35879162/registereventhotkey-from-thread