开发者

How to check current mouse button state using Win32/User32 library?

I know how to stimulate clicks using User32 SendInput method and what I need is a similar User32 method but to obtain the current mouse button state.

Something similar to:

public static extern bool GetCursorPos(ref System.Drawing.Point lpPoint);

Function GetCursorPos gives me the current cursor position. What I need is the left button state (if it's clicked or not). Is there s开发者_JS百科uch a function?


Use GetAsyncKeyState, To Quote MSDN:

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).


There's a method called GetAsyncKeyState. The method signature looks like this:

[DllImport("user32.dll")] public static extern short GetAsyncKeyState(UInt16 virtualKeyCode);

Then you simply call it passing the left mouse key code (VK_LBUTTON = 0x01) and off you go.

More information directly from MSDN.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜