Skip to content

moveMouse refactor to use sendInput on Windows (#26) #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 10, 2021
29 changes: 26 additions & 3 deletions src/win32/mouse.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../mouse.h"
#include "../screen.h"
#include "../microsleep.h"

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

/**
* This constant is required as Windows divides the entire
* screen space into 65536 segments in both X and Y axes
* irrespective of resolution
* https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-mouseinput#remarks
*/
#define ABSOLUTE_COORD_CONST 65536


#define MMMouseToMEventF(down, button) \
(down ? MMMouseDownToMEventF(button) : MMMouseUpToMEventF(button))

Expand All @@ -20,13 +30,26 @@
: ((button) == RIGHT_BUTTON ? MOUSEEVENTF_RIGHTDOWN \
: MOUSEEVENTF_MIDDLEDOWN))

MMPoint CalculateAbsoluteCoordinates(MMPoint point) {
MMSize displaySize = getMainDisplaySize();
return MMPointMake((point.x / displaySize.width) * ABSOLUTE_COORD_CONST, (point.y / displaySize.height) * ABSOLUTE_COORD_CONST);
}

/**
* Move the mouse to a specific point.
* @param point The coordinates to move the mouse to (x, y).
*/
void moveMouse(MMPoint point)
{
SetCursorPos ((int)point.x, (int)point.y);
void moveMouse(MMPoint point) {
INPUT mouseInput;
MMPoint pointAbsolute = CalculateAbsoluteCoordinates(point);
mouseInput.type = INPUT_MOUSE;
mouseInput.mi.dx = pointAbsolute.x;
mouseInput.mi.dy = pointAbsolute.y;
mouseInput.mi.mouseData = 0;
mouseInput.mi.time = 0;
mouseInput.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
mouseInput.mi.dwExtraInfo = 0;
SendInput(1, & mouseInput, sizeof(mouseInput));
}

void dragMouse(MMPoint point, const MMMouseButton button)
Expand Down