Simulating Raw Input on Windows

北城余情 提交于 2019-12-08 11:15:21

问题


I have an application that does some complicated things with raw input and I would like to have some automated tests for it. This is the code that sends input albeit chopped down a bit to make it easier to follow.

Sending:

GetRawInputDeviceList(nullptr, &numDevices, sizeof(RAWINPUTDEVICELIST));
rawInputDeviceList = new RAWINPUTDEVICELIST[numDevices];
GetRawInputDeviceList(rawInputDeviceList, &numDevices, sizeof(RAWINPUTDEVICELIST));

HGLOBAL hRaw = ::GlobalAlloc(GHND, sizeof(RAWINPUT));
RAWINPUT *pRaw = reinterpret_cast<RAWINPUT*>(::GlobalLock(hRaw));
pRaw->data.keyboard.MakeCode = 30;
pRaw->data.keyboard.Flags = 0;
pRaw->data.keyboard.Reserved = 0;
pRaw->data.keyboard.VKey = 0x43;
pRaw->data.keyboard.Message = WM_KEYDOWN;
pRaw->data.keyboard.ExtraInformation = 0;
pRaw->header.dwType = RIM_TYPEKEYBOARD;
pRaw->header.dwSize = sizeof(RAWINPUT);
pRaw->header.hDevice = rawInputDeviceList[0].hDevice;
pRaw->header.wParam = (WPARAM)RIM_INPUT;
::GlobalUnlock(hRaw);

SendMessage(hWnd_, WM_INPUT, (WPARAM)RIM_INPUT, (LPARAM)hRaw);

Receiving:

case WM_INPUT:
  UINT size = 0;  
  if(GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER)))
    return GetLastError();

The return value from GetLastError() is ERROR_INVALID_HANDLE(6). However I can read the handle fine if I use GlobalLock it myself.

According to http://msdn.microsoft.com/en-us/library/windows/desktop/ms645596(v=vs.85).aspx "lParam: A handle to the RAWINPUT structure. This comes from the lParam in WM_INPUT."

I've tried just using a pointer and I get the same error message. I've tried with/without unlocking, as well as using LocalAlloc/LocalLock instead. I've tried with PostMessage and it's all the same result, and I am now out of ideas.


回答1:


The WM_INPUT message cannot be sent between processes. Like WM_PAINT, it is a synthesized message which can only be properly generated by the system itself. The lParam is not just an HGLOBAL containing the data, it is an opaque handle understood only by the raw input code.

Raw Input messages are generated directly by the GetMessage/PeekMessage internals, and the associated objects are cleaned up by DefWindowProc. SendMessage will not marshal the LPARAM for WM_INPUT, because the code to do so wasn't written as it is not intended to be used in that way.



来源:https://stackoverflow.com/questions/22591604/simulating-raw-input-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!