Skip to content

Instantly share code, notes, and snippets.

@markscottwright
Last active September 29, 2020 20:53
Show Gist options
  • Save markscottwright/7ad7fcb2e20e5c6b3faed533123fa2da to your computer and use it in GitHub Desktop.
Save markscottwright/7ad7fcb2e20e5c6b3faed533123fa2da to your computer and use it in GitHub Desktop.
How to move a mouse cursor to a particular monitor
#include <windows.h>
#include <stdio.h>
#include <string.h>
RECT monitors[100];
int numMonitors = 0;
BOOL SaveMonitorInfo(
HMONITOR Arg1,
HDC Arg2,
LPRECT Arg3,
LPARAM Arg4)
{
monitors[numMonitors++] = *Arg3;
return TRUE;
}
int main(int argc, char** argv) {
EnumDisplayMonitors(
NULL,
NULL,
SaveMonitorInfo,
NULL);
int whichMonitor = argc > 1 ? atoi(argv[1]) : -1;
for (int i=0; i < numMonitors; ++i) {
int x = (monitors[i].right-monitors[i].left)/2 + monitors[i].left;
int y = (monitors[i].bottom-monitors[i].top)/2 + monitors[i].top;
if (whichMonitor == i || (whichMonitor < 0
&& monitors[i].left == 0 && monitors[i].top == 0)) {
printf("moving to monitor #%d\n", i);
SetPhysicalCursorPos(x, y);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment