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

I know how to stimulate clicks using the User32 SendInput method, and I need a similar User32 method, but to get the current state of the mouse button.

Something like:

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

The GetCursorPos function gives me the current cursor position. I need the state of the left button (if it clicked or not). Is there such a function?

+3
source share
2 answers

There is a GetAsyncKeyState method. The signature of the method is as follows:

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

Then you just call it passing the left mouse key code (VK_LBUTTON = 0x01) and release it.

Additional information directly from MSDN .

+3

GetAsyncKeyState, MSDN:

GetAsyncKeyState . , , . , GetAsyncKeyState (VK_LBUTTON) , , . GetSystemMetrics (SM_SWAPBUTTON).

+3

Source: https://habr.com/ru/post/1751491/


All Articles