I have this simple Windows C++ program using WinAPI:
#include <windows.h>#include <windowsx.h> // GET_Y_LPARAM#include <string>#include <iostream>// clang++ -std=c++23 mouse.cc -o mouse.exe -luser32 -lgdi32// Handler for WM_MOUSEMOVEvoid OnMouseMove(HWND hwnd, int x, int y) { MOUSEMOVEPOINT inputPoint; ZeroMemory(&inputPoint, sizeof(inputPoint)) ; inputPoint.x = x;// & 0x0000FFFF ; inputPoint.y = y;// & 0x0000FFFF ; //std::cerr << x << " " << y << std::endl; //inputPoint.time = GetMessageTime(); // Use the time of the current message //inputPoint.dwExtraInfo = GetMessageExtraInfo(); MOUSEMOVEPOINT points[64]; // Adjust size based on your needs int pointsRetrieved = GetMouseMovePointsEx(sizeof(MOUSEMOVEPOINT), &inputPoint, points, 64, GMMP_USE_DISPLAY_POINTS); if (pointsRetrieved > 0) { std::cerr << pointsRetrieved << std::endl; } else { //std::cerr << "Error retrieving mouse move points. Error: " << GetLastError() << std::endl; }}LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);int MyWindowCreation(HINSTANCE hInstance) { const char CLASS_NAME[] = "Sample Window Class"; WNDCLASS wc = {}; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); HWND hwnd = CreateWindowEx( 0, CLASS_NAME,"Mouse Position Display", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); if (hwnd == NULL) { return 0; } ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd); MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0;}LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_MOUSEMOVE: { int xPos = GET_X_LPARAM(lParam); int yPos = GET_Y_LPARAM(lParam); OnMouseMove(hwnd, xPos, yPos); std::string mousePosition = "X: " + std::to_string(xPos) +", Y: " + std::to_string(yPos); SetWindowText(hwnd, mousePosition.c_str()); //OnMouseMove(hwnd, xPos, yPos); break; } } return DefWindowProc(hwnd, uMsg, wParam, lParam);}int main() { HINSTANCE hInstance = GetModuleHandle(NULL); MyWindowCreation(hInstance); return 0;}
That i put together to try to get mouse data using GetMouseMovePointsEx
. I tried different things but I keep getting pointsRetrieved=-1
and the error message says that it can find the mouse data for that position in the buffer.
I looked at the MS documentation for this function. Sadly, the code they provided couldn't compile (it doesn't respect the function signature). At least this code compiles). I tried with & 0x0000FFFF
and without setting x
and y
, but it makes no difference. The coordinates passed from WM_MOUSEMOVE
are correct.
It must be something simple!? Can it be that somehow it would be hardware-related? I mean maybe on some configuration, Windows just doesn't record the mouse history? I am on Windows 10.
Is there an alternative, better way to do this? Is this an outdated method?