CGEvents to emulate "Command-V"(paste command) ?

Discussion in 'Mac Programming' started by laphroaig309, Oct 13, 2010.

  1. macrumors newbie

    Joined:
    Oct 1, 2010
    #1
    Hi:

    I used the following CGEventRef trying to emulate the "Command-V"
    key strokes, and that did not work as I wished.

    Can you help me to see anything go wrong?
    //Command Virtual KeyCode ==> 55
    //v ==> 9

    pKeyboardEvent1 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)55, YES) ;
    pKeyboardEvent2 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)9, YES) ;
    pKeyboardEvent3 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)9, NO) ;
    pKeyboardEvent4 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)55, NO) ;

    // Null pointer check ignored here for briefing...

    CGEventPost( kCGHIDEventTap, pKeyboardEvent1);
    CGEventPost( kCGHIDEventTap, pKeyboardEvent2);
    CGEventPost( kCGHIDEventTap, pKeyboardEvent3);
    CGEventPost( kCGHIDEventTap, pKeyboardEvent4);

    //Below are 4 CFRelease for the above CGEventRef...

    Thank you very much.
     

Share This Page