2

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?

Felierix
  • 179
  • 3
  • 12
Ufx
  • 2,595
  • 12
  • 44
  • 83

2 Answers2

11

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • your solution always sends message to 0,0, not to x,y. – Ufx Feb 08 '15 at 07:04
  • The second and third inputs do not include the `MOUSEEVENTF_ABSOLUTE` flag, so they are relative to the mouse position established by the first input. This is stated in the documentation: "If the MOUSEEVENTF_ABSOLUTE value is not specified, dx and dy specify movement relative to the previous mouse event (the last reported position)." So it is OK that dx and dy are 0 in the second and third inputs. – Remy Lebeau Feb 08 '15 at 07:35
  • For the record, non of the comments tell you how to correctly specify the coordinates. They hint at it, but you need to experiment a little. – Dan May 02 '19 at 18:15
  • @Dan as I stated in my answer, the `MOUSEINPUT` documentation explains exactly how to specify the mouse coordinates, eg: "*If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain **normalized absolute coordinates** between 0 and 65,535. The event procedure maps these coordinates onto the display surface. Coordinate (0,0) maps onto the upper-left corner of the display surface; coordinate (65535,65535) maps onto the lower-right corner. **In a multimonitor system, the coordinates map to the primary monitor**.*" – Remy Lebeau May 02 '19 at 20:13
  • 1
    @RemyLebeau You mean the "Remarks" section on MSDN. And no they dont "EXPLAIN" anything. You and I may know what y=mx+b is but it is not explained to anyone. Thats why there are a dozen pages just like this, where the answers are inadequate. – Dan May 03 '19 at 22:46
0

You can use mouse_event function to click on (x,y):

mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
Mustafa Chelik
  • 2,113
  • 2
  • 17
  • 29
  • 3
    `mouse_event()` has been superceded by [`SendInput()`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310.aspx), you should be using that instead. The documentation you linked to even says so. – Remy Lebeau Feb 08 '15 at 03:36
  • @MustafaChelik Your solution always sends messages to cursor coordinates, not to x,y. – Ufx Feb 08 '15 at 06:59