Take a look at the comments in CGEventSource.h. Itβs a little easier to move the information together than using the help of the event services . A long but more correct way looks like creating an event source (which obeys the rules of memory management, you need CFRelease it if you finished using it before the program ended):
myEventSource = CGEventSourceCreate(kCGEventSourceStatePrivate);
This will create your own event source with a unique identifier; you indicate that the events you created came from there:
CGEventRef myKeyboardEvent = CGEventCreateKeyboardEvent(myEventSource, keyCode, true);
When an event arrives, check to see if it happened on your own:
if( (CGEventGetType(newEvent) == kCGEventKeyDown) && (CGEventGetIntegerValueField(newEvent, kCGEventSourceStateID) == CGEventSourceGetSourceStateID(myEventSource) ) {
There is also a user data field for the source, which allows you to bypass arbitrary 64 bits if you need to.
A quick and dirty way is to try to select an event field that is unlikely to be a significant value for a keyboard event, for example kCGMouseEventPressure and turn it into a signature:
CGEventSetIntegerValueField(myKeyboardEvent, kCGMouseEventPressure, 0xFEEDFACE); // The field is an int_64 so you could make the sig longer
Josh caswell
source share