监控任何应用程序中的选择

发布于 2024-09-26 07:20:41 字数 295 浏览 0 评论 0原文

我想监视用户在任何应用程序中所做的所有文本选择。这可能吗?我更喜欢 .net 中的解决方案,但普通 C++ 也可以。

如果没有,我可以从 .net 应用程序监控所有文本复制操作 (CTRL+C) 吗?

类似的问题: 在C#中,有没有办法始终能够获取当前焦点窗口的选定文本内容?

I want to monitor all text selections made in any application by the user. Is that possible? I would prefer a solution in .net, but vanilla C++ is OK.

If not, can I monitor all text copy operations (CTRL+C) from a .net application?

similar question: In C#, is there a way to consistently be able to get the selected text contents of currently focused window?

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

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

发布评论

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

评论(2

橘味果▽酱 2024-10-03 07:20:41

“选择”不是一个通用的概念,每个控件可以以自己的方式处理它。如果您想拦截每个选择,您可以在 Windows 消息上放置一个全局挂钩,并通过过滤掉那些与“已知”编辑控件(标准编辑控件、RichEdit 控件等)相关的通知来拦截通知。看起来不错并检查源窗口类(编辑框的选择更改可能是其他控件的核战争开始的通知)。您不会获得所有的选择(例如,Word中的选择不会被拦截),但您可能会认为获得其中的绝大多数。

但是有一个大问题:无窗口控件。无窗口控件,正如术语本身所说,不是窗口,因此它们没有 HWND 或任何东西;实际上,它们可以被认为是在屏幕上绘制的像素,而不需要来自应用程序外部的任何附加接口(它们通常是 COM 东西)。您无法挂钩它们,也无法对它们进行子类化,并且由于它们通常使用 COM 接口来通知其所有者 (随机示例),您无法对其所有者进行子类化以获取通知。当然,可能有一些奇怪的方法来获取它们的内容,但它可能涉及到每个进程中的 dll 注入,甚至比为“正常”编辑框提出的方法更不通用。

由于无窗口控件非常普遍(例如,使用 DirectUI 的浏览器和应用程序使用它们来避免浪费大量的 HWND,IIRC Office 也使用它们,WPF 应用程序使用它们自己风格的无窗口控件, VB6/Delphi应用程序可以使用它们,...),你会错过很多选择,所以我建议你遵循复制拦截的方式,这样更简单,更安全。

在这方面,您可以遵循@Richard的建议并使用AddClipboardFormatListener API。请记住,此功能仅适用于 Windows Vista 及以上版本,因此,如果您希望与以前的 Windows 版本兼容,则应使用较旧的“剪贴板查看器”API 集。一些信息 这里


Edit

呃,我忘了它,我正在调查 Active Accessibility 是否有效(尽管 通常没有正确实施)可以帮助您完成这项任务,并且有 这个有希望的方法,但事实证明它仅适用于选定的子对象,并且,一般来说,

客户注意事项 Active Accessibility 不会公开编辑和富编辑控件中的文本选择。

因此,我认为,如果即使可访问性也不能提供此类信息,那么就很难有任何其他标准化方法来获取它。

A "selection" is not a universal concept, each control may handle it in its own way. If you want to intercept every selection, you could place a global hook on windows messages, and intercept the notifications relative to "known" edit controls (the standard edit control, the RichEdit control, ...), by filtering out the ones which look good and checking the source window class (what is a select change for an Edit box may be the notification of the start of a nuclear war for some other control). You won't get all the selections (e.g. selections in Word won't be intercepted), but you may think to get the great majority of them.

But there's a big problem: windowless controls. Windowless controls, as the term itself says, aren't windows, so they haven't got HWNDs or anything; actually, they can just be thought as pixel drawn on the screen without any additional interface from the extern of the application (they are usually COM stuff). You can't hook them, you can't subclass them, and since they usually use COM interfaces to notify their owner (random example), you can't subclass their owner to get their notifications. Sure, probably there is some strange method to get their content, but it probably involves dll injection in each process and is even less general than the method proposed for "normal" edit boxes.

Since windowless controls are quite widespread (browsers and applications which use DirectUI, for example, use them to avoid wasting tons of HWNDs, IIRC Office too uses them, WPF applications use their own flavor of windowless controls, VB6/Delphi applications can use them, ...), you'll have a lot of selections missed, so I advise you to follow the copy intercept way, which is much simpler and safer.

In this respect, you can follow @Richard's advice and use the AddClipboardFormatListener API. Keep in mind that this is available only from Windows Vista onwards, so, if you want to be compatible with previous Windows versions, you should use the older "Clipboard Viewer" set of APIs. Some information here.


Edit

Uh, I was forgetting about it, I was investigating if Active Accessibility (although often not properly implemented) could help you in this task, and there was this promising method, but it turns out that it's only for selected sub-objects, and that, in general,

Note to clients Active Accessibility does not expose the text selection in edit and rich edit controls.

So, I think that, if even accessibility doesn't provide such information, it's pretty difficult that there's any other standardized way to get it.

绾颜 2024-10-03 07:20:41

Register for and process WM_CLIPBOARDUPDATE messages in a (hidden) window.

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