Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c#

winapi

mouse

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?

like image 940
Alex Avatar asked Dec 28 '22 12:12

Alex


1 Answers

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

like image 122
Necrolis Avatar answered Jan 13 '23 15:01

Necrolis