原始输入鼠标 - 未收到数据[关闭](Raw Input mouse - No data received [closed])

我从键盘和鼠标中获取原始数据 - 键盘数据正在接收到我的应用程序,我完全没有遇到任何麻烦。 不知何故,我的应用程序没有收到任何鼠标数据。 实际上,它甚至没有传递if (raw->header.dwType == RIM_TYPEMOUSE)子句。

case WM_CREATE:{ /* ... other initialisations ... */ // register for raw data rid[0].dwFlags=RIDEV_NOLEGACY|RIDEV_INPUTSINK; // ignore legacy messages rid[0].usUsagePage=1; rid[0].usUsage=6; // keyboard rid[0].hwndTarget=hWnd; rid[1].dwFlags=RIDEV_NOLEGACY; // ignore legacy messages rid[1].usUsagePage=1; rid[1].usUsage=2; // mouse rid[1].hwndTarget=hWnd; RegisterRawInputDevices(rid,2,sizeof(RAWINPUTDEVICE)); break; } case WM_INPUT:{ if(GetRawInputData((HRAWINPUT)lParam,RID_INPUT,NULL,&dwSize,sizeof(RAWINPUTHEADER))==-1){ break; } LPBYTE lpb=new BYTE[dwSize]; if(lpb==NULL){ break; } if(GetRawInputData((HRAWINPUT)lParam,RID_INPUT,lpb,&dwSize,sizeof(RAWINPUTHEADER))!=dwSize){ delete[] lpb; break; } PRAWINPUT raw=(PRAWINPUT)lpb; UINT KeyEvent; if (raw->header.dwType == RIM_TYPEKEYBOARD) { KeyEvent=raw->data.keyboard.Message; keyChar=MapVirtualKey(raw->data.keyboard.VKey,MAPVK_VK_TO_CHAR); if(KeyEvent==WM_KEYDOWN){ /* Works great */ } } else if (raw->header.dwType == RIM_TYPEMOUSE) { WriteFile(hFile, TEXT_ENTER, strlen(TEXT_ENTER), &fWritten, 0); // TEXT_ENTER contains "Mouse event occured" // Even this never gets executed. My file never gets logged with this message LMButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_LEFT_BUTTON_DOWN; MMButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_MIDDLE_BUTTON_DOWN; RMButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_RIGHT_BUTTON_DOWN; if (LMButtonDown) WriteFile(hFile, LCLICK, strlen(LCLICK), &fWritten, 0); // LCLICK contains "Left mouse button clicked" if (MMButtonDown) WriteFile(hFile, MCLICK, strlen(MCLICK), &fWritten, 0); // MCLICK contains "Middle mouse button clicked" if (RMButtonDown) WriteFile(hFile, RCLICK, strlen(RCLICK), &fWritten, 0); // RCLICK contains "Right mouse button clicked" */ } delete[] lpb; CloseHandle(hFile); break; }

任何人都可以发现任何错误,并告诉我它是什么?

I'm getting raw data from both the keyboard and the mouse - the keyboard data is being received to my application perfectly, I'm having no trouble at all. Somehow, none of the mouse data is being received to my application. In fact, it doesn't even pass the if (raw->header.dwType == RIM_TYPEMOUSE) clause.

case WM_CREATE:{ /* ... other initialisations ... */ // register for raw data rid[0].dwFlags=RIDEV_NOLEGACY|RIDEV_INPUTSINK; // ignore legacy messages rid[0].usUsagePage=1; rid[0].usUsage=6; // keyboard rid[0].hwndTarget=hWnd; rid[1].dwFlags=RIDEV_NOLEGACY; // ignore legacy messages rid[1].usUsagePage=1; rid[1].usUsage=2; // mouse rid[1].hwndTarget=hWnd; RegisterRawInputDevices(rid,2,sizeof(RAWINPUTDEVICE)); break; } case WM_INPUT:{ if(GetRawInputData((HRAWINPUT)lParam,RID_INPUT,NULL,&dwSize,sizeof(RAWINPUTHEADER))==-1){ break; } LPBYTE lpb=new BYTE[dwSize]; if(lpb==NULL){ break; } if(GetRawInputData((HRAWINPUT)lParam,RID_INPUT,lpb,&dwSize,sizeof(RAWINPUTHEADER))!=dwSize){ delete[] lpb; break; } PRAWINPUT raw=(PRAWINPUT)lpb; UINT KeyEvent; if (raw->header.dwType == RIM_TYPEKEYBOARD) { KeyEvent=raw->data.keyboard.Message; keyChar=MapVirtualKey(raw->data.keyboard.VKey,MAPVK_VK_TO_CHAR); if(KeyEvent==WM_KEYDOWN){ /* Works great */ } } else if (raw->header.dwType == RIM_TYPEMOUSE) { WriteFile(hFile, TEXT_ENTER, strlen(TEXT_ENTER), &fWritten, 0); // TEXT_ENTER contains "Mouse event occured" // Even this never gets executed. My file never gets logged with this message LMButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_LEFT_BUTTON_DOWN; MMButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_MIDDLE_BUTTON_DOWN; RMButtonDown = raw->data.mouse.ulButtons & RI_MOUSE_RIGHT_BUTTON_DOWN; if (LMButtonDown) WriteFile(hFile, LCLICK, strlen(LCLICK), &fWritten, 0); // LCLICK contains "Left mouse button clicked" if (MMButtonDown) WriteFile(hFile, MCLICK, strlen(MCLICK), &fWritten, 0); // MCLICK contains "Middle mouse button clicked" if (RMButtonDown) WriteFile(hFile, RCLICK, strlen(RCLICK), &fWritten, 0); // RCLICK contains "Right mouse button clicked" */ } delete[] lpb; CloseHandle(hFile); break; }

Can anybody spot any mistake, and tell me what it is?

最满意答案

将标志RIDEV_INPUTSINK添加到dwFlags是一个简单的例子。 我的应用程序不在前台 - 此标志使您可以接收系统范围的输入(您的应用程序无需关注以接收输入)。

It was a simple case of adding the flag RIDEV_INPUTSINK to dwFlags. My application isn't in the foreground - and this flag enables you to receive system-wide input (your application need not be in focus to receive the input).

更多推荐