如何发送“Cmd-C”在 Objective-C 中对活动应用程序进行击键,或者告诉应用程序执行复制操作?

发布于 2024-08-06 07:30:23 字数 549 浏览 2 评论 0原文

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

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

或者甚至更好:如何告诉活动应用程序执行复制操作而不发送击键,或者在触发复制操作时不突出显示“编辑”菜单。

我无法使用 NSAppleScript (10.2),因为这个特定的类必须尽可能向后兼容(理想情况下低至 10.0)。

我看过 NSAppleEvents (这似乎是可行的方法),但似乎找不到一种方法来做到这一点。非常欢迎具体的答案!

提前致谢!

更新:

Rob 的 NSAppleScript 解决方案可以工作,但我仍然有兴趣在不需要 NSAppleScript 的情况下完成此操作。我尝试使用 CGPostKeyboardEvent (使用最后一个布尔值将键设置为其“向上”或“向下”状态),但我似乎找不到键代码或任何字符代码表。有人可以精确说明如何使用 CGPostKeyboardEvent 来输入“c”吗? (CGCharCode 和 CGKeyCode 表就完美了)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宣告ˉ结束 2024-08-13 07:30:23

如果您可以在计算机上启用系统事件(访问辅助设备),则可以使用 NSAppleEvent [编辑:实际上是 NSAppleScript] initWithSource 发送此脚本:

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

不过,我真的不知道何时引入系统事件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文