tags:

views:

23

answers:

2

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 such a function?

+1  A: 

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

Necrolis
A: 

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.

Alex