我正在构建一个将鼠标输入转换为虚拟操纵杆的应用程序,我尝试过做低级鼠标输入,但是获取鼠标位置还不够,所以我选择了rawinput,但现在我卡住了,我无法检索数据来自鼠标。
int main(int argc, char** argv)
{
while (true) {
printf("%d %d\n", mouse_x, mouse_y);
Sleep(100);
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CREATE:
RAWINPUTDEVICE Rid[2];
Rid[0].usUsagePage = 0x01;
Rid[0].usUsage = 0x02;
Rid[0].dwFlags = RIDEV_INPUTSINK; // adds HID mouse and also ignores legacy mouse messages
Rid[0].hwndTarget = 0;
Rid[1].usUsagePage = 0x01;
Rid[1].usUsage = 0x06;
Rid[1].dwFlags = RIDEV_INPUTSINK; // adds HID keyboard and also ignores legacy keyboard messages
Rid[1].hwndTarget = 0;
return 0;
case WM_INPUT:
{
UINT dwSize;
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));
LPBYTE lpb = new BYTE[dwSize];
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER));
RAWINPUT* raw = (RAWINPUT*)lpb;
if (raw->header.dwType == RIM_TYPEKEYBOARD)
{
}
else if (raw->header.dwType == RIM_TYPEMOUSE)
{
mouse_x = raw->data.mouse.lLastX;
mouse_y = raw->data.mouse.lLastY;
}
delete[] lpb;
break;
}
}
}