Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I build a Windows application that intercepts and passes through keystrokes and mouse clicks to other applications?

I'm working with a customer who wants to add functionality to a legacy application. The application, whose vendor is useless, has a Windows Forms UI. What my customer wants is for certain functionality outside this application to be triggered when the user clicks on a piece of information in the application.

I've seen a simple version of this done for another customer. In this case, there's an app consisting of a form that's been set to a topmost window using SetWindowPos. When the user clicks a button on this form, the app finds the window for the legacy app and gets information out of the window's caption. It's inelegant - there's this floating button that never goes away even if the legacy app isn't open - but it works.

I was wondering if it would be possible to do something similar to this using a borderless transparent WPF window with the Topmost property set. The app I'm thinking of would analyze the content in the legacy app's window and define a list of hotspots. It would intercept and handle any mouse click in a hotspot, and pass all remaining mouse clicks through to the legacy app.

I'm not terrifically experienced with the Windows API, so I don't know if it's straightforward (or even possible) to implement this kind of capability. And it occurs to me that if I were authoring anti-malware tools, the application I have in mind is exactly the kind of thing I'd be trying to cripple.

If this is actually a viable project, what's the best way to approach doing it? What unexpected problems should I be looking out for?

like image 813
Robert Rossney Avatar asked Apr 20 '11 18:04

Robert Rossney


People also ask

Which type of program uses Windows hooks?

A computer-based training (CBT) application uses this hook procedure to receive useful notifications from the system. An application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function before calling the hook procedures associated with any type of hook.

What is Wh_cbt?

The WH_CBT hook is intended primarily for computer-based training (CBT) applications. For more information, see the CBTProc callback function. For information, see WinEvents.

What is a hook procedure?

A hook is a mechanism by which an application can intercept events, such as messages, mouse actions, and keystrokes. A function that intercepts a particular type of event is known as a hook procedure. A hook procedure can act on each event it receives, and then modify or discard the event.

Are Command key accelerators faster than a menu?

Accelerators provide faster, more direct access to commands than menus do. At a minimum, an application should provide accelerators for the more commonly used commands. Although accelerators typically generate commands that exist as menu items, they can also generate commands that have no equivalent menu items.


2 Answers

This can be done without any trickery with overlaying buttons by means of WH_CALLWNDPROC and/or WH_GETMESSAGE hooks.

like image 144
David Heffernan Avatar answered Sep 24 '22 16:09

David Heffernan


I would definitely try UI Automation. It may not work, but at least, it should be fairly easy to try. And, most of the time, if it does not work, it means there won't be any other "easy" way.

UI Automation finds its root in assistive technologies for disabled people, which is most of the time what we want to do with apps we don't own: do things from an external application, without being able to really do it "the standard way".

It also logically has the notion of events, see the officiel doc here: UI Automation Events Overview

like image 25
Simon Mourier Avatar answered Sep 21 '22 16:09

Simon Mourier