Posting synthetic events

4 views
Skip to first unread message

Dave DeLong

unread,
Feb 17, 2012, 12:33:42 AM2/17/12
to cocoaheads---...@googlegroups.com
Below is some code that posts a "c" character event.  The event codes are in <HIToolbox/Events.h>.  If you're looking to target event posts to a specific process (as opposed to globally in the sample below), you should use the CGEventPostToPSN() function instead.

CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
if (eventSource != NULL) {

    

    CGEventRef eventDown = CGEventCreateKeyboardEvent(eventSource, (CGKeyCode)0x08, YES);
    CGEventPost(kCGSessionEventTap, eventDown);
    CFRelease(eventDown);

    

    CGEventRef eventUp = CGEventCreateKeyboardEvent(eventSource, (CGKeyCode)0x08, NO);
    CGEventPost(kCGSessionEventTap, eventUp);
    CFRelease(eventUp);

    

    CFRelease(eventSource);
}

Cheers,

Dave
Reply all
Reply to author
Forward
0 new messages