Mouse input in WPF applications

I worked on input input in a WPF application. What makes this project difficult is that I need to be able to enter input into the application, even if it is running in the background (that is, another application has input focus). Therefore, the use of the SendInput () function is out of the question.

While my keyboard is working, but I have problems with mouse input.

I used Spy ++ to monitor window messages that are sent to the WPF window when I physically click the mouse. Then I simply process the same mouse messages (e.g. WM_LBUTTONDOWN and WM_LBUTTONUP ) manually and explicitly send them to the WPF window to emulate mouse input.

Unfortunately, this does not work properly (even if I did not set the WPF window as the foreground for testing purposes).

I added a button to my test WPF window, which when clicked displays a message box. Entering the corresponding mouse messages when I manually positioned the cursor over the button does not cause the button to be pressed, however (i.e., the Click event is not triggered by the WPF framework).

If I add a mouse click handler to the actual dialog box (client area), this handler will trigger a call if I place the cursor over the dialog itself and add the same window messages as before:

 this.MouseLeftButtonDown += WndMouseDown; public void WndMouseDown(object sender, EventArgs e) { ... } 

Oddly enough, if I changed the button click mode to click (that is, assuming I clicked the mouse down, and not the default mouse), the button click event now fires when I insert the same messages as before. (It is worth mentioning that the handler from the above example works correctly for both the mouse and ups, so it would seem that the WPF environment successfully processes both messages.)

It seems that there are some other criteria that must be met in order for the click event to be fired using the WPF environment. Does anyone know how mouse input is handled internally by WPF, or why doesn't it interpret my mouse up and down messages as a click on a button?

(It’s worth mentioning that this approach [sending window messages] works fine in regular Win32 windows, such as the Start-> Run dialog. The difference is that WPF has only one physical Win32 window, and the rest means that so that all window messages go to this top-level window, and not to the actual button.)

I searched high and low to answer this question and would appreciate any thoughts or ideas.

+6
wpf mouse inject
Sep 03 '09 at 3:20
source share
3 answers

I highly recommend switching to the UIAutomation route. You create an AutomationElement using a window handle. Go to the button and call it. I just wanted to know how you got to work with the keyboard. I am currently trying to solve the reverse problem. How to get a WPF window (I managed to get hWnd to it through Win32 calls) in order to respond to virtual keyboard messages. I started spy sessions on Windows in the window in question and played it without success.

+3
Oct. 06 '09 at 3:33
source share

Use UI Automation for this - an attempt to manually simulate input data through window messages - a textbook error, for example, an attempt to start a ground war against Russia.

+1
Sep 04 '09 at 3:39
source share

Your strategy basically sounds, but in order to send a message to a window belonging to another process, you must first register the message.

Here is an article explaining the whole business . The sample code is, unfortunately, in VB, but I'm sure this will not stop you.

0
Sep 04 '09 at 4:01
source share



All Articles