How to send a “Cmd-C” keystroke to the active application in objective-c, or tell the application to do a copy operation?

若如初见. 提交于 2019-12-23 05:23:34

问题


Getting the active application is not a problem, I already have this implemented.

What I want to know is how to send a "Cmd-C" key combination to the active application so it puts the selected text in the clipboard (general pasteboard).

Or even better: how to tell the active application to do a copy operation without sending the keystroke, or without highlighting the "Edit" menu when the copy operation is triggered.

I can't use NSAppleScript (10.2) because this specific class must be as backwards-compatible as possible (down to 10.0 ideally).

I have looked at NSAppleEvents (which would seem the way to go) but can't seem to find a way to do this. A specific answer would be very welcome!

Thanks in advance!

UPDATE:

The NSAppleScript solution by Rob works but I'm still interested in doing this without the need for NSAppleScript. I've tried using CGPostKeyboardEvent (using the last boolean value to set the key to its "up" or "down" status) but I can't seem to find the key codes or any character code tables. Could someone precise how CGPostKeyboardEvent would be used to type a "c"? (a CGCharCode and CGKeyCode table would be perfect).


回答1:


See GCPostKeyboardEvent.




回答2:


If you can enable System Events (access for assistive devices) on the computer, you can send this script with an NSAppleEvent [EDIT: actually NSAppleScript] initWithSource:

tell application "System Events" to keystroke "c" using command down

I don't really have any idea when System Events was introduced, though.



来源:https://stackoverflow.com/questions/1505933/how-to-send-a-cmd-c-keystroke-to-the-active-application-in-objective-c-or-tel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!