Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if mouse button is down

I am new to c++ and I am trying to activate a line of code only when the left mouse button is held down. In this example, my code works but it seems that it just toggles it. When I click, it spams the H key then, when I click again, it stops.

Currently I have this code:

if ((GetKeyState(VK_LBUTTON)))
{
    keybd_event(VkKeyScan('H'),0,0,0);
    Sleep ( 30 );
}

Edit:

I have inside the function:

int WINAPI WinMain ( HINSTANCE hInst, HINSTANCE P, LPSTR CMD, int nShowCmd );
like image 919
Jordan B Avatar asked Aug 19 '13 16:08

Jordan B


2 Answers

Use this to determine if the button is pressed.

if((GetKeyState(VK_LBUTTON) & 0x8000) != 0)

http://vcpptips.wordpress.com/tag/vk_lbutton/

like image 119
pippin1289 Avatar answered Sep 28 '22 22:09

pippin1289


The application can catch messages and process being sent to your window indicating a state change of any mouse button.

When the left button is pressed a

WM_LBUTTONDOWN

is sent.

When it is released

WM_LBUTTONUP

is sent.

Please read here for various messages being sent to indicate mouse events.

like image 21
alk Avatar answered Sep 28 '22 23:09

alk