Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I simulate mouse events from code?

Tags:

I would like to simulate mouse events using the Win32 API; how can I do it?

What I want to do is simulate the event at the most basic level, the level at which the system has just the event type and the co-ordinates and hasn't yet figured which window it must relay it to.

I don't know if that's how things work. Either way, I need help doing it. Would I have to meddle at the driver level?!

To make my requirements clear, I don't want to target any window, I just want the system to think the mouse was clicked or moved by the user. And I would be coding in C.

like image 677
Dharma Avatar asked Mar 02 '11 07:03

Dharma


People also ask

How do you simulate a mouse event?

An easier and more standard way to simulate a mouse click would be directly using the event constructor to create an event and dispatch it. Though the MouseEvent. initMouseEvent() method is kept for backward compatibility, creating of a MouseEvent object should be done using the MouseEvent() constructor.

How do you simulate mouse clicks?

You can right-click by holding down the left mouse button. This is useful if you find it difficult to move your fingers individually on one hand, or if your pointing device only has a single button.

What are mouse events in Windows programming?

This event occurs when the mouse pointer is over the control and the user presses a mouse button. The handler for this event receives an argument of type MouseEventArgs. This event occurs when the mouse pointer enters the border or client area of the control, depending on the type of control.

What raises an event when the mouse button is released?

MouseEnter Raised when the cursor enters a control while no mouse button is pressed. MouseHover Raised when the mouse cursor hovers over a control. MouseLeave Raised when the mouse cursor leaves a control. Raised when the mouse cursor moves over a control.


2 Answers

You're looking for the SendInput function, which allows you to synthesize mouse movements and button clicks in your code by specifying an array of INPUT structures corresponding to input events.

UINT WINAPI SendInput(   __in  UINT nInputs,     // number of structures in the pInputs array   __in  LPINPUT pInputs,  // an array of INPUT structures, representing an event   __in  int cbSize        // the size, in bytes, of an INPUT structure ); 

Note, however, that this function is subject to User Interface Privilege Isolation (UIPI), which means that your application is only permitted to inject input to applications that are running at an equal or lesser integrity level.

like image 197
Cody Gray Avatar answered Oct 08 '22 09:10

Cody Gray


Use mouse_event (winuser.h). The following code will move the mouse then perform a click at the new location. You can do this in two lines but this is more verbose.

Note that X and Y are specified in mickeys, 0 to 65535. This is then mapped onto the current resolution, i.e. 0,0 will be the top left corner and 65535,65535 will be the lower right hand corner.

mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0); mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); 
like image 37
Chris Avatar answered Oct 08 '22 09:10

Chris