views:

174

answers:

3

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?

A: 

I'm not sure I understand the relationship between your code and the application's code. Are you injecting or something? Is there a reason you can't do: [[window firstResponder] copy:nil]?

Describe a little more what you're doing, please.

Colin Barrett
He's trying to remotely tell another application to copy whatever is selected to the clipboard.
Chuck
correct. I have no relationship with the application's code.
Ben Packard
A: 

Rather than posting raw key events, you might be better off using the Accessibility APIs to trigger the Copy item in the other app's Edit menu. This will probably be more reliable.

Rob Keniger
Except not everyone has the Accessibility APIs enabled. You can enable/disable them by going to System Preferences > Universal Access > "Enable Access for assistive devices"
Dave DeLong
No dice - the application I'm trying to copy from hasn't implemented any accessibility data (purposefully). And it doesn't have an edit menu, but only responds to command-c or a rightclick>copy.
Ben Packard