objective c - Working with events for processing global hotkeys on Mac OS X -
what want:
i have program running. when program in tray , out of focus, want have couple of global shortcuts set send messages program. mean "send messages"? well, inside program, want have access flag, indicate state specified key-pair (fired or not). poll flag in loop , take decision there.
what found:
system-wide hotkey application
what not understand:
from links above looks have pass handler when registering hotkey. on hotkey press, os calls handler. right? not understand how in world system call handler inside program if program running.
i think main problem not understand how mac programming done in days before objective c , cocoa became norm. before that, programming done in c (or c++) using carbon. name used library supposed "carbon" copy of more modern set of apis during transition between mac os (classic) , mac os x.
another thing have understand registration of hotkeys given in examples give above must paired registration of carbon event handler invoked when hit hotkey combination.
that said, think should read legacy document carbon event manager:
and pay particular attention how carbon events supposed registered. particularly use:
osstatus installeventhandler(eventtargetref target, eventhandlerupp handlerproc, uint32 numtypes, const eventtypespec* typelist, void* userdata, eventhandlerref* handlerref); the way use made objective c wrapper in following:
this part of class, let's call myowneventhandler:
- (eventhandlerref)handlerref { if ( handlerref == nil ) { nsassert( installeventhandler(getapplicationeventtarget(), &eventhandler, 0, nil, self, &handlerref ) == noerr, @"handlerref" ); } return handlerref; } // carbon callback os invokes when app gets // hotkey event must handled osstatus eventhandler( eventhandlercallref inhandler, eventref inevent, void* inuserdata ) { eventhotkeyid hotkeyid; geteventparameter( inevent, keventparamdirectobject, typeeventhotkeyid, nil, sizeof(eventhotkeyid, nil, &hotkeyid ) // use myowneventhandler object if need // reason why because passed self in installeventhandler // in carbon event callbacks cannot access self directly // because c callback, not objective c method myowneventhandler* handler = (myowneventhandler *)inuserdata; // handle hotkey here - store id of eventhotkeyid struct // in objective c hotkey object events in array of registered hotkeys return eventnothandlederr; // return error other handlers handle event } // call objective c wrapper method register carbon event handler - (void)registerforgettinghotkeyevents { const eventtypespec khotkeysevent[] = {{ keventclasskeyboard, keventhotkeypressed }}; addeventtypestohandler( [self handlerref], geteventtypecount(khotkeysevent), khotkeysevent ); } // call objective c wrapper method unregister carbon event handler - (void)unregisterfromgettinghotkeyevents { const eventtypespec khotkeysevent[] = {{ keventclasskeyboard, keventhotkeypressed }}; removeeventtypesfromhandler( [self handlerref], geteventtypecount(khotkeysevent), khotkeysevent ); } i hope helps. if stuck somewhere let me know , try you.
Comments
Post a Comment