各种输入API的问题(RAWINPUT,WINAPI)

时间:2013-07-14 01:02:33

标签: c++ windows winapi raw-input windows-messages

背景

在使用Windows API开发的大部分时间里,我一直在使用WM_MOUSEMOVEWM_KEYDOWN / WM_KEYUP消息分别用于鼠标和键盘输入。但是,我最近开始实现一个输入类,它使用RAWINPUTWM_INPUT消息来处理输入。

由于site

,键盘实现正在顺利运行

但是我现在把注意力转向实现鼠标移动。根据我的理解,WM_INPUT在处理鼠标时会生成鼠标移动增量,而不是客户空间坐标。

问题是我还想在类中存储鼠标指针的客户端和屏幕空间坐标,理想情况下我想这样做而不需要捕获WM_MOUSEMOVE消息或使用{ {1}} / GetCursorPos()如果可能,由于我(可能是无根据的)对性能问题的恐惧而起作用。

我对ScreenToClient()的研究引导了我here,其中说明了鼠标的详细信息:

  

请注意,返回的值不是您从WM_MOUSEMOVE消息中获得的屏幕空间值。这是我们得到的原始数据,因此值需要解释。

这使我相信通过使用初始调用RAWINPUT计算鼠标光标的初始位置然后对GetCursorPos()消息检测到的每个鼠标移动事件,可以解释数据,将从WM_INPUT消息中检索到的增量添加到初始光标位置,并采取措施确保位置保持在屏幕边界内。例如这样的事情(注意这不会编译,因为它包含我班级的摘录,但它应该足以概述我目前正在做的事情)

WM_INPUT

问题是通过此方法为// Somewhere in class header RECT m_screenMouseBounds; POINT m_mouseDelta; POINT m_mouseScreenPosition; POINT m_mouseScreenPositionGETCURSOR; RAWMOUSE m_rawMouse; // ... // Somewhere during class initialisation GetCursorPos(&m_mouseScreenPosition); // Get initial mouse cursor position // Determine maximum and minimum boundaires of current display m_screenMouseBounds.right = GetSystemMetrics(SM_CXSCREEN); m_screenMouseBounds.bottom = GetSystemMetrics(SM_CYSCREEN); m_screenMouseBounds.top = 0; m_screenMouseBounds.left = 0; // Prepare the devices for registering RAWINPUTDEVICE theInputDevices[2]; // 0 = keyboard theInputDevices[0].usUsagePage = 0x01; theInputDevices[0].usUsage = 0x06; theInputDevices[0].dwFlags = RIDEV_NOLEGACY; theInputDevices[0].hwndTarget = theWindowToHandle; // 1 = mouse theInputDevices[1].usUsagePage = 0x01; theInputDevices[1].usUsage = 0x02; theInputDevices[1].dwFlags = 0; // RIDEV_NOLEGACY theInputDevices[1].hwndTarget = theWindowToHandle; // Register each device with raw input if(RegisterRawInputDevices(theInputDevices,2,sizeof(RAWINPUTDEVICE))==FALSE) { // throw exception return false; } // ... // In the WM_INPUT processing function with parameters : (WPARAM wParam, LPARAM lParam) char inputBuffer[sizeof(RAWINPUT)] = {}; UINT inputBufferSize = sizeof(RAWINPUT); GetRawInputData(reinterpret_cast<HRAWINPUT>(lParam), RID_INPUT, inputBuffer, &inputBufferSize, sizeof(RAWINPUTHEADER)); RAWINPUT* rawData = reinterpret_cast<RAWINPUT*>(inputBuffer); if(rawData->header.dwType == RIM_TYPEMOUSE) { m_rawMouse = rawData->data.mouse; // Add the movement deltas acquired from the WM_INPUT message m_mouseDelta.x = m_rawMouse.lLastX; m_mouseDelta.y = m_rawMouse.lLastY; // Update the screen position using the delta values (Does not work as expected!) m_mouseScreenPosition.x += m_mouseDelta.x; m_mouseScreenPosition.y += m_mouseDelta.y; // Ensure mouse coords are within the screens boundaries if (m_mouseScreenPosition.x < m_screenMouseBounds.left) { m_mouseScreenPosition.x = m_screenMouseBounds.left; } if (m_mouseScreenPosition.x > m_screenMouseBounds.right) { m_mouseScreenPosition.x = m_screenMouseBounds.right; } if (m_mouseScreenPosition.y < m_screenMouseBounds.top) { m_mouseScreenPosition.y = m_screenMouseBounds.top; } if (m_mouseScreenPosition.y > m_screenMouseBounds.bottom) { m_mouseScreenPosition.y = m_screenMouseBounds.bottom; } // Double check to make sure that the screen position coordinates calculated // above match the screen coordinates as determined by Windows // using GetCursorPos() (Hint: They don't! :( ) GetCursorPos(&m_mouseScreenPositionGETCURSOR); TCHAR s[256]; swprintf(s, L"MouseDX: %ld MouseDY: %ld\n", m_mouseDelta.x, m_mouseDelta.y); OutputDebugString(s); swprintf(s, L"MouseX(Screen(WM_INPUT)): %ld MouseY(Screen(WM_INPUT)): %ld\n", m_mouseScreenPosition.x, m_mouseScreenPosition.y); OutputDebugString(s); swprintf(s, L"MouseX(Screen(GetCursorPos)): %ld MouseY(Screen(GetCursorPos)): %ld\n", m_mouseScreenPositionGETCURSOR.x, m_mouseScreenPositionGETCURSOR.y); OutputDebugString(s); } 获取的值与通过WINAPI函数m_mouseScreenPosition获取的m_mouseScreenPositionGETCURSOR的值不同。

总结

我想我的一整套问题是:

  1. GetCursorPos()结构相关的lLastXlLastY的值是什么?它们与屏幕坐标/客户端坐标有关,即返回的值与屏幕上的像素的关系是1:1,或者它们与显示器完全无关,而且与鼠标的细节(灵敏度等)相关,因此不是1 :1与像素的关系?

  2. 如果1的答案与鼠标而不是显示坐标有关:是否可以仅使用RAWMOUSE来确定鼠标指针的实际屏幕坐标?或者我是否必须捕获WM_INPUT消息和/或使用GetCursorPos()来确定客户端空间和屏幕空间中的游标坐标?

  3. 如果对2的回答是,我将需要使用WM_MOUSEMOVE / WM_MOUSEMOVE:如果我最终使用GetCursorPos(),我是否应该注意任何性能问题以及RAWINPUT / WM_MOUSEMOVE?或者我对表现的担忧没有根据?

  4. 如果您已做到这一点,请感谢您,我感谢您提供给我的任何帮助或信息。谢谢!

1 个答案:

答案 0 :(得分:0)

  1. “请注意,返回的值不是您从WM_MOUSEMOVE消息中获得的屏幕空间值。这是我们获取的原始数据,因此值需要解释。这些值通常相对于最后一个位置,因此表明确保你可以检查RAWMOUSE结构中的usFlags。“从玩具制造商得到这个,我总是觉得很棒。 Here

  2. RAWINPUT仅用于获取相对输入。这就是为什么它的原始。如果你真的不想使用GetCursorPos,只需使用WM_MOUSEMOVE并检查l / w参数。

  3. 我从未真正对GetCursorPos()进行过性能测试,但我已经为自己的项目做了一些阅读,我从未见过任何反对意见。调用GetCursorPos,无论你是否使用RAWINPUT,每帧一次或每次更新肯定都可以忽略不计。我用它来旋转相机,它一直很好。