Wasabi Fan Wasabi Fan - 3 months ago 7
C++ Question

Getting precise mouse movement deltas while application is not in foreground

I am trying to figure out how to get precise mouse movement information from the Win32 API in a way that still works when my application is not focused. So far, I have tried three different approaches:


  1. Registering for
    WM_MOUSEMOVE
    events through
    SetWindowsHookEx
    with
    WH_MOUSE_LL
    . This works while the application is in the background, but returns screen coordinates which are relatively low-precision. Also, pointer ballistics are applied to these values, making them less than ideal for my purposes.

  2. Calling
    GetCursorPos
    in a loop. This also works in the background, but again, it returns low-resolution screen coordinates and pointer acceleration is applied.

  3. Registering for raw input via
    RegisterRawInputDevices
    and then watching for
    WM_INPUT
    events (with calls to
    GetRawInputData
    to actually access the data). This one -- at least, according to the docs -- returns coordinates in native mouse units, so it is as precise as possible. Unfortunately, this only works when the application is focused. The docs say:


    An application can receive data when it is in the foreground and when it is in the background.


    But I have not seen any behavior that corroborates these claims.



Since my main question is regarding the third option, I have included my code for that one below.

So, how can I access the native mouse values in a way that works even when the application doesn't have focus? Is there extra configuration needed to make #3 work, or are there other options?

#include "stdafx.h"
// ...

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name

HWND mainWindow;

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.

// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_MOUSETEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MOUSETEST));

MSG msg;

// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MOUSETEST));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_MOUSETEST);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassExW(&wcex);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable

mainWindow = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | RIDEV_INPUTSINK,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

if (!mainWindow)
{
return FALSE;
}

ShowWindow(mainWindow, nCmdShow);
UpdateWindow(mainWindow);

// Register for raw input events

RAWINPUTDEVICE targetDevice = { 0x01, 0x02, 0, mainWindow };
BOOL registerRawSuccess = RegisterRawInputDevices(&targetDevice, 1, sizeof(RAWINPUTDEVICE));

if (!registerRawSuccess)
return FALSE;

return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
// ...
case WM_INPUT :
{
UINT dwSize = 40;
static BYTE lpb[40];

if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT,
NULL, &dwSize, sizeof(RAWINPUTHEADER)) == -1)
{
// TODO: Handle this... something broke
}

UINT result = GetRawInputData((HRAWINPUT)lParam, RID_INPUT,
lpb, &dwSize, sizeof(RAWINPUTHEADER));

RAWINPUT* raw = (RAWINPUT*)lpb;

if (raw->header.dwType == RIM_TYPEMOUSE)
{
// Use raw->data.mouse.lLastX and raw->data.mouse.lLastY
}
break;
}
// ...
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

Answer

When using the Raw Input API, specify the RIDEV_INPUTSINK flag if you want to receive WM_INPUT messages while your app is running in the background:

If set, this enables the caller to receive the input even when the caller is not in the foreground. Note that hwndTarget must be specified.

Change this line:

RAWINPUTDEVICE targetDevice = { 0x01, 0x02, 0, mainWindow };

To this:

RAWINPUTDEVICE targetDevice = { 0x01, 0x02, RIDEV_INPUTSINK, mainWindow };
Comments