我对GetRawInputBuffer有意见。代码不返回错误,但检索到的响应中没有数据。
我已经根据这个Using GetRawInputBuffer correctly编写了代码
UINT RawInputSize;
UINT Result;
Result = GetRawInputBuffer(NULL, &(RawInputSize), sizeof(RAWINPUTHEADER));
if (Result == -1)
{
DWORD ErrorCode = GetLastError();
return;
}
UINT AllocatedBufferByteCount = RawInputSize * 16;
RAWINPUT* RawInputBuffer = reinterpret_cast<RAWINPUT*>(malloc(AllocatedBufferByteCount));
UINT AllocatedBufferByteCountTwo = AllocatedBufferByteCount;
Result = GetRawInputBuffer(RawInputBuffer, &(AllocatedBufferByteCountTwo), sizeof(RAWINPUTHEADER));
if (Result == -1)
{
DWORD ErrorCode = GetLastError();
return;
}
UINT RawInputCount = Result;
RAWINPUT* RawInput = RawInputBuffer;
for (unsigned int i = 0; i < RawInputCount; ++i)
{
switch (RawInput->header.dwType)
{
case RIM_TYPEMOUSE:
{
this->UpdateMouse(RawInput->data.mouse);
break;
}
case RIM_TYPEKEYBOARD:
{
this->UpdateKeyboard(RawInput->data.keyboard);
break;
}
}
RawInput = NEXTRAWINPUTBLOCK(RawInput);
}
DefRawInputProc(&(RawInputBuffer), RawInputCount, sizeof(RAWINPUTHEADER));此代码在case WM_INPUT外部调用。RawInputCount始终为零。如果我在case WM_INPUT中使用GetRawInputData,我可以正确地接收数据。
这段代码有什么问题?为什么我的结果是空的?
发布于 2018-02-04 06:40:31
答案来得有点晚,但由于我最近遇到了同样的问题,我无论如何都会回答这个问题: GetRawInputBuffer使用WM_INPUT消息来获取和缓冲消息。但是,您可能会使用while(PeekMessage(&Message, NULL, 0, 0, PM_REMOVE))之类的东西来处理窗口消息,它会在发送到应用程序之后删除所有消息。这样,输入消息将不会发送到GetRawInputBuffer方法,并且该方法将始终返回大小为0的值。为了解决这个问题,你需要使用类似下面这样的东西作为你的主循环:
//Process and remove all messages before WM_INPUT
while(PeekMessage(&Message, NULL, 0, WM_INPUT - 1, PM_REMOVE))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
//Process and remove all messages after WM_INPUT
while(PeekMessage(&Message, NULL, WM_INPUT + 1, (UINT)-1, PM_REMOVE))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}这样,您的应用程序既不会处理或删除WM_INPUT消息,也不会将其发送到GetRawInputBuffer。显然,您也可以与PM_NOREMOVE交换PM_REMOVE标志,但是这可能会导致其他问题,因此我建议使用上面的方法来处理和删除除WM_INPUT消息之外的所有消息。
https://stackoverflow.com/questions/28879021
复制相似问题