PDA

View Full Version : CGEvents to emulate "Command-V"(paste command) ?




laphroaig309
Oct 14, 2010, 01:45 AM
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.