b2cloud

    21st February 2014

    Simulating keyboard events in OSX

    Guides | Tutorial By 3 years ago

    I’m currently writing a program which takes input and simulates it back out as keyboard presses.

    It’s quite easy, and can be done like so: (this simulates Command-C, a copy shortcut)

    A list of character codes for each keyboard key can be found here

    const CGKeyCode keyCode = 8;
    
    CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
    CGEventRef eventDown = CGEventCreateKeyboardEvent(source, keyCode, YES);
    CGEventSetFlags(eventDown, kCGEventFlagMaskCommand);
    CGEventRef eventUp = CGEventCreateKeyboardEvent(source, keyCode, NO);
    
    CGEventPost(kCGSessionEventTap, eventDown);
    
    //	Keep it down for a bit
    sleep(1);
    
    CGEventPost(kCGSessionEventTap, eventUp);
    
    CFRelease(eventUp);
    CFRelease(eventDown);
    CFRelease(source);
    Recommended Posts

    Yammer integrations in ReactJS

    Post by 3 years ago

    I am writing this blog while I am working on a project for our client’s intranet website. The client requires the website has the ability to share, like and write comments in the website through

    Got an idea?

    We help entrepreneurs, organizations and established brands from around
    the country bring ideas to life. We would love to hear from you!