Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate mouse click using C++? [closed]

I need to simulate a mouse click that clicks on application windows. I'm using Windows.

How can I send a left button mouse click to screen x, y coordinates where the window is located?

like image 751
Ufx Avatar asked Oct 23 '25 20:10

Ufx


1 Answers

Use the SendInput() function:

INPUT Inputs[3] = {0};

Inputs[0].type = INPUT_MOUSE;
Inputs[0].mi.dx = ...; // desired X coordinate
Inputs[0].mi.dy = ...; // desired Y coordinate
Inputs[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;

Inputs[1].type = INPUT_MOUSE;
Inputs[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;

Inputs[2].type = INPUT_MOUSE;
Inputs[2].mi.dwFlags = MOUSEEVENTF_LEFTUP;

SendInput(3, Inputs, sizeof(INPUT));

Be sure to read the comments in the MOUSEINPUT documentation regarding how to specify dx and dy correctly when using MOUSEEVENTF_ABSOLUTE in a multi-monitor environment.

like image 129
Remy Lebeau Avatar answered Oct 25 '25 11:10

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!