Sending a 'copy' command in cocoa

Posted by Ben Packard on Stack Overflow See other posts from Stack Overflow or by Ben Packard
Published on 2010-01-11T16:36:19Z Indexed on 2010/05/22 13:50 UTC
Read the original article Hit count: 231

Filed under:
|
|

Is there any way to send a copy (command-c) instruction without using a cgEvent to mimic the keystrokes? I don't have access to the text field in the application I want to take text from, so need to replicate manually copying to the clipboard, and there seemss to be a bug with cgevent posting.

According the Quartz documentation, to type a capital Z I should use:

CGEventRef event1, event2, event3, event4;
event1 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, true);
event2 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, true);
event3 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)6, false);
event4 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, false);

However, if I attach this set of instuctions to an NSTimer, it only works the first time it is fired. example output:

ZzzZzzzzZZzzZ (vs expected ZZZZZZZZZ).

How else might I send a copy command to the active window?

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about event