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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With