如何发送"Cmd-C"消息在Objective-C中对活动应用程序的击键操作,还是告诉该应用程序执行复制操作? [英] How to send a "Cmd-C" keystroke to the active application in objective-c, or tell the application to do a copy operation?

查看:77
本文介绍了如何发送"Cmd-C"消息在Objective-C中对活动应用程序的击键操作,还是告诉该应用程序执行复制操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取活动应用程序不是问题,我已经实现了.

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

我想知道的是如何将"Cmd-C"组合键发送到活动应用程序,以便将所选文本放入剪贴板(常规粘贴板)中.

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.

我不能使用NSAppleScript(10.2),因为此特定类必须尽可能地向后兼容(理想情况下应降至10.0).

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

我看过NSAppleEvents(这似乎是可行的方法),但似乎找不到解决方法.一个具体的答案将非常受欢迎!

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!

提前谢谢!

更新:

Rob的NSAppleScript解决方案可以工作,但是我仍然对此感兴趣,而无需NSAppleScript.我尝试使用CGPostKeyboardEvent(使用最后一个布尔值将键设置为上"或下"状态),但似乎找不到键代码或任何字符代码表.有人可以精确说明如何使用CGPostKeyboardEvent键入"c"吗? (一个CGCharCode和CGKeyCode表将是完美的).

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秒登录
发送“验证码”获取 | 15天全站免登陆