Simulate Mouse move/click/keyPress in an applicati

2019-03-22 07:42发布

问题:

I know how to simulate mouse and keyboard events, but they act as if the user did them, so they will affect the window that is active. What I need is to simulate one of those inputs, but in a Window that is not active.

I'm not saying that it is minimized, imagine for example, you have msPaint, and notepad. Notepad is in front of paint. And you want to simulate mouse clicks in certain coordinates of the paint window, but without setting it active, making it possible for the user to keep using notepad which is in fron of paint.

Is this possible at all? Thanks!

回答1:

You might do the following:

1) get the HWND of the window inside which you want to simulate events. In order to do this you can use the FindWindow function, which takes two parameters, one being the window's class name (you can use NULL) and the other the name of the window (see MSDN documentation for details);

2) send a message to the desired window using SendMessage function. The parameters are: the previous obtained window handle (HWND), the message (which can be for example WM_KEYDOWN, or WM_MOUSEMOVE, WM_LBUTTONDOWN etc.), the WPARAM of the message (which in case of WM_KEYDOWN is the virtual key code of the key) and finally the LPARAM of the message (which in case of WM_KEYDOWN is quite complex and you should check MSDN for details).

In my opinion this is sufficient for what you intend to do.



回答2:

Well if you knew JAVA you could use the robots class.

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed

Java robots class

Maybe theres something simular in .net?