开发者

Monitor selections in any application

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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜