Skip to content

Commit dd463e7

Browse files
authored
Merge pull request #82 from Reiss-Cashmore/develop
moveMouse refactor to use sendInput on Windows (#26)
2 parents 0f88d28 + ea13e11 commit dd463e7

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/win32/mouse.c

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "../mouse.h"
2+
#include "../screen.h"
23
#include "../microsleep.h"
34

45
#include <math.h> /* For floor() */
@@ -7,6 +8,15 @@
78
#define M_SQRT2 1.4142135623730950488016887 /* Fix for MSVC. */
89
#endif
910

11+
/**
12+
* This constant is required as Windows divides the entire
13+
* screen space into 65536 segments in both X and Y axes
14+
* irrespective of resolution
15+
* https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-mouseinput#remarks
16+
*/
17+
#define ABSOLUTE_COORD_CONST 65536
18+
19+
1020
#define MMMouseToMEventF(down, button) \
1121
(down ? MMMouseDownToMEventF(button) : MMMouseUpToMEventF(button))
1222

@@ -20,13 +30,26 @@
2030
: ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTDOWN \
2131
: MOUSEEVENTF_MIDDLEDOWN))
2232

33+
MMPoint CalculateAbsoluteCoordinates(MMPoint point) {
34+
MMSize displaySize = getMainDisplaySize();
35+
return MMPointMake((point.x / displaySize.width) * ABSOLUTE_COORD_CONST, (point.y / displaySize.height) * ABSOLUTE_COORD_CONST);
36+
}
37+
2338
/**
2439
* Move the mouse to a specific point.
2540
* @param point The coordinates to move the mouse to (x, y).
2641
*/
27-
void moveMouse(MMPoint point)
28-
{
29-
SetCursorPos ((int)point.x, (int)point.y);
42+
void moveMouse(MMPoint point) {
43+
INPUT mouseInput;
44+
MMPoint pointAbsolute = CalculateAbsoluteCoordinates(point);
45+
mouseInput.type = INPUT_MOUSE;
46+
mouseInput.mi.dx = pointAbsolute.x;
47+
mouseInput.mi.dy = pointAbsolute.y;
48+
mouseInput.mi.mouseData = 0;
49+
mouseInput.mi.time = 0;
50+
mouseInput.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
51+
mouseInput.mi.dwExtraInfo = 0;
52+
SendInput(1, & mouseInput, sizeof(mouseInput));
3053
}
3154

3255
void dragMouse(MMPoint point, const MMMouseButton button)

0 commit comments

Comments
 (0)