objective c - How to tell the difference between a user-tapped keyboard event and a generated one? -


i've installed keyboard hook:

cgeventref mycgeventcallback(cgeventtapproxy proxy, cgeventtype type, cgeventref event, void *refcon) {

basically want take user's keyboard taps, eat input, , post own input instead.

so if taps "g" might want post "foo" textfield.

i'm writing textfield cgeventpost , cgeventsetunicodestring found here: http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg23343.html

the problem each of programmatically entered characters hitting keyboard hook. can't return null in keyboard hook block user input... blocks program's input well!

i differentiated them on windows side in c# 'injected' flag, see question year ago here: how use low-level 8 bit flags conditionals?

looking similar in objective-c.

take @ comments in cgeventsource.h. it's little bit easier put information using event services reference. long, more correct, way around looks creating event source (which subject memory management rules; need cfrelease if you're done using before program termination):

myeventsource = cgeventsourcecreate(kcgeventsourcestateprivate); 

this create own private event source unique id; indicate events create came there:

cgeventref mykeyboardevent = cgeventcreatekeyboardevent(myeventsource,                                                          keycode, true); 

when event comes in, check see if it's yourself:

 if( (cgeventgettype(newevent) == kcgeventkeydown) &&      (cgeventgetintegervaluefield(newevent, kcgeventsourcestateid) == cgeventsourcegetsourcestateid(myeventsource) ) { 

there's user data field source lets pass around arbitrary 64 bits, should need to.

the quick , dirty way try picking event field isn't meaningful value keyboard event, kcgmouseeventpressure , turn signature:

cgeventsetintegervaluefield(mykeyboardevent,                              kcgmouseeventpressure,                              0xfeedface); // field int_64 make sig longer 

Comments