Check if Mouse LButton is down?

我的未来我决定 提交于 2019-12-07 05:30:46

问题


How do I check if the Left button of my mouse is currently pressed down/dragging something(I preffer the first possibility).

I tried Mouse.IsDraging,but no result.

NOTE: I handle mouse messages in my application so its no problem if its a WM,just share a way to accomplish my task.


回答1:


There is a Windows API function GetAsyncKeyState(), which despite its name is also usable to get the state of the mouse buttons. The linked documentation directly contains the answer to your question:

The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling GetSystemMetrics(SM_SWAPBUTTON) which returns TRUE if the mouse buttons have been swapped.

The result type is short, to check for the most significant bit just test whether the value is negative.




回答2:


OnMouseMove(UINT nFlags, CPoint point)
{
  m_LButtonPressed=nFlags & MK_LBUTTON;
  CWnd::OnMouseMove(nFlags, point);
} 


来源:https://stackoverflow.com/questions/1275894/check-if-mouse-lbutton-is-down

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