RegisterEventHotKey from thread

懵懂的女人 提交于 2019-12-13 00:56:08

问题


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 the everyEvent argument set to 0 - 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-CH204

    gotEvent = 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/RunCurrentEventLoop

  • CFRunLoopRun - https://developer.apple.com/library/ios/documentation/CoreFoundation/Reference/CFRunLoopRef/index.html#//apple_ref/c/func/CFRunLoopRun
  • CFRunLoopInMode - 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!