在应用程序不在前台时获得精确的鼠标移动增量
Getting precise mouse movement deltas while application is not in foreground
我正在尝试弄清楚如何从 Win32 API 获取精确的鼠标移动信息,这种方式在我的应用程序未获得焦点时仍然有效。到目前为止,我已经尝试了三种不同的方法:
- 通过
SetWindowsHookEx
和 WH_MOUSE_LL
注册 WM_MOUSEMOVE
事件。这在应用程序处于后台时有效,但 returns 屏幕坐标精度相对较低。此外,指针弹道学应用于这些值,使它们不太适合我的目的。
- 循环调用
GetCursorPos
。这也适用于后台,但它再次应用 returns 低分辨率屏幕坐标和指针加速。
通过 RegisterRawInputDevices
注册原始输入,然后观察 WM_INPUT
事件(调用 GetRawInputData
以实际访问数据)。这个 - 至少,根据文档 - returns 以本地鼠标单位坐标,因此它尽可能精确。不幸的是,这仅在应用程序获得焦点时有效。 The docs 说:
An application can receive data when it is in the foreground and when it is in the background.
但我还没有看到任何证实这些说法的行为。
因为我的主要问题是关于第三个选项,所以我在下面包含了我的代码。
那么,即使应用程序没有焦点,我如何才能以一种有效的方式访问本机鼠标值?是否需要额外的配置才能使 #3 工作,或者是否有其他选项?
#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;
}
使用原始输入 API 时,如果您想在后台 运行 时接收 WM_INPUT
消息,请指定 RIDEV_INPUTSINK
标志:
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.
更改此行:
RAWINPUTDEVICE targetDevice = { 0x01, 0x02, 0, mainWindow };
为此:
RAWINPUTDEVICE targetDevice = { 0x01, 0x02, RIDEV_INPUTSINK, mainWindow };
我正在尝试弄清楚如何从 Win32 API 获取精确的鼠标移动信息,这种方式在我的应用程序未获得焦点时仍然有效。到目前为止,我已经尝试了三种不同的方法:
- 通过
SetWindowsHookEx
和WH_MOUSE_LL
注册WM_MOUSEMOVE
事件。这在应用程序处于后台时有效,但 returns 屏幕坐标精度相对较低。此外,指针弹道学应用于这些值,使它们不太适合我的目的。 - 循环调用
GetCursorPos
。这也适用于后台,但它再次应用 returns 低分辨率屏幕坐标和指针加速。 通过
RegisterRawInputDevices
注册原始输入,然后观察WM_INPUT
事件(调用GetRawInputData
以实际访问数据)。这个 - 至少,根据文档 - returns 以本地鼠标单位坐标,因此它尽可能精确。不幸的是,这仅在应用程序获得焦点时有效。 The docs 说:An application can receive data when it is in the foreground and when it is in the background.
但我还没有看到任何证实这些说法的行为。
因为我的主要问题是关于第三个选项,所以我在下面包含了我的代码。
那么,即使应用程序没有焦点,我如何才能以一种有效的方式访问本机鼠标值?是否需要额外的配置才能使 #3 工作,或者是否有其他选项?
#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;
}
使用原始输入 API 时,如果您想在后台 运行 时接收 WM_INPUT
消息,请指定 RIDEV_INPUTSINK
标志:
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.
更改此行:
RAWINPUTDEVICE targetDevice = { 0x01, 0x02, 0, mainWindow };
为此:
RAWINPUTDEVICE targetDevice = { 0x01, 0x02, RIDEV_INPUTSINK, mainWindow };