如何使用 Cocoa 将文本从一个应用程序粘贴到另一个应用程序? [英] How to paste text from one app to another using Cocoa?

查看:17
本文介绍了如何使用 Cocoa 将文本从一个应用程序粘贴到另一个应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Apple 文档中阅读了有关 NSPasteBoard 的信息,以及它如何允许应用程序写入 PasteBoard 并允许其他应用程序读取该文本并使用它.

I have read about NSPasteBoard in the Apple documentation, and how it allows for applications to write into the PasteBoard and allow other applications to read that text and use it.

谁能告诉我如何将文本从应用程序(位于状态栏中)粘贴到另一个应用程序内的 NSTextField 中.

Could someone tell me how to paste text from am application (that sits in the status bar) into a NSTextField that is inside a different application.

我想做的是类似于 SnippetSnippetsApp 做.

What I am trying to do is something similar what Snippet and SnippetsApp do.

如果我完全愚蠢并且错过了 Apple Docs 中的明显内容,请您指出正确的路径:)

If I am completely stupid and missed the obvious in Apple Docs, could you please point me to the right path :)

谢谢!

推荐答案

有人能告诉我如何将来自应用程序(位于状态栏中)的文本粘贴到另一个应用程序内的 NSTextField 中.

Could someone tell me how to paste text from am application (that sits in the status bar) into a NSTextField that is inside a different application.

粘贴是在接收应用程序中发生的.写入粘贴板就是复制.

Pasting is what happens in the receiving application. Writing to a pasteboard is copying.

此外,您不能假设用户想要粘贴到 NSTextField 中.它可能是 NSTextView,或 WebView 中的 textarea,或 Carbon EditText 或 MLTE 控件,或其他一些文本编辑器,例如 Qt 或 wxWidgets 文本编辑器.他们甚至可能正在使用带有列表视图的应用程序,让他们可以将文本直接粘贴到其中.

Furthermore, you can't assume that the user will want to paste into an NSTextField. It may be an NSTextView, or a textarea in a WebView, or a Carbon EditText or MLTE control, or some other text editor such as a Qt or wxWidgets text editor. They may even be using an app with a list view that lets them paste text directly into it.

因此,没有编程方式可以直接告诉应用程序这里有一些文本 - 请粘贴".您必须将其复制到通用粘贴板,然后伪造一个通常会导致最前面的应用程序粘贴的事件.Charlie 对 ⌘V 的建议是一种方式,虽然很棘手;Dvorak 布局将 V 放在另一个键上,而Dvorak QWERTY ⌘"布局将 V-with-⌘(相对于 V-without-⌘)放在与 QWERTY 的 V 相同的键上.

So, there's no programmatic way to directly tell an application "here's some text — paste it, please". You have to copy it to the general pasteboard and then forge an event that should generally cause the frontmost app to paste. Charlie's suggestion of ⌘V is one way, albeit tricky; the Dvorak layout puts V on another key, while the "Dvorak QWERTY ⌘" layout puts V-with-⌘ (as opposed to V-without-⌘) on the same key as QWERTY's V.

要伪造 ⌘V 事件,请查看 CGEventTap.您需要使用 CGEventCreateKeyboardEvent 函数 来创建事件本身,由于该函数需要一个键码,因此您需要查找 V 部分的正确键码⌘V 组合,这将需要通过 文本输入源服务键盘布局服务,具体取决于布局.

To forge that ⌘V event, look into CGEventTap. You'll need to use the CGEventCreateKeyboardEvent function to create the event itself, and since that function takes a key code, you'll need to look up the proper key code for the V part of the ⌘V combination, which will require going through Text Input Source Services or Keyboard Layout Services, depending on the layout.

此时您可能会考虑使用辅助功能在编辑"菜单中找到粘贴"菜单项并向其发送 AXPress 消息,但粘贴"和编辑"只是这些概念的英文单词;如果您这样做,您的应用程序将无法以任何其他语言运行.您可以按顺序(第三个菜单,第六个菜单项),但是您的应用程序将无法在没有文件菜单、没有重做菜单项或有两个撤消菜单项 (Photoshop) 的应用程序中运行.打造一个⌘V事件真的是要走的路.

You might at this point think of using Accessibility to find the Paste menu item in the Edit menu and send it an AXPress message, but "Paste" and "Edit" are only English's words for those concepts; if you did this, your app would not work in any other language. You could go by order (third menu, sixth menu item), but then your app would not work in applications without a File menu, or without a Redo menu item, or with two Undo menu items (Photoshop). Forging a ⌘V event really is the way to go.

这篇关于如何使用 Cocoa 将文本从一个应用程序粘贴到另一个应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆