Releasing keys

Hello.

Does anyone knows how to detect whether the user has pressed the left mouse click and later released it?

I don't need the GetAsyncKeyState(VK_LBUTTON) because it only tells me that the key is pressed. But what if the key isn't released the moment it was pressed?
Is there a way to detect that? Like (VK_LBUTTONDOWN) and (VK_LBUTTONUP) or something?

Thanks.
Last edited on
For that, you would have to handle the WM_LBUTTONDOWN and WM_LBUTTONUP messages. If you only care about the time the key is released, WM_BUTTONUP will suffice.

Both work like this:
hWnd : Handle to the window that sent the message.
Message: WM_LBUTTONUP or WM_BUTTONDOWN obviously.
wParam: Modifiers like the alt or the ctrl key. Can be accessed with
wParam & MK_CTRL for example. For a full list, see: http://msdn.microsoft.com/en-us/library/ms645607%28VS.85%29.aspx

lParam: Holds the mouse coordinates. LOWORD is the x coordinate, HIWORD the y coordinate.
This works with Console Applications too, right?
Thanks!
I made this little program.

1
2
3
4
5
6
7
8
9
10
11
12
	int primary = 0;

while(primary != 1){
	if (true)  {
         if (GetAsyncKeyState(WM_LBUTTONDOWN)) {

			 cout << "Key doooown!" << endl;


		 }
	}
}


When I press my left click nothing happens.
Why doesn't it work?
Last edited on
Um, because WM_*ANYTHING* is a window message. So no, it doesn't work with console applications. The thing is, catching mouse events in a console application doesn't make all too much sense in the first place, so I just assumed you had a window.
So, is it even possible to do this in a console application?
Well, yes. But you'd have to keep polling GetASyncKeyState:


If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState.


In theory, you could check the least significant bit. But since windows is a multitasking OS, you do NOT do that (another application could call GetASyncKeyState before you), instead you do it with the polling method:

You check GetASyncKeyState in a loop. If GetASyncKeyState(VK_RBUTTON) is not 0, you set a variable that you could call rightState or something like that to true. If it is 0, you check whether rightState is true or not, if it is the user has pressed and released the right button - you set rightState to false and do whatever you want to do with the right mouse button being released.

This is unreliable for 2 reasons:
It doesn't work if the user clicks and release faster than your program takes for one loop.
It doesn't work if the user clicks, released and then clicks again faster than your program.

BUT, since on modern machines you can do this so fast that no human could really keep up, you can usually ignore both of these.

But really, if you are already programming for windows, and want mouse input, why do you not want to use a window? *scratches head* makes no sense to me.
Last edited on
What you want is probably RegisterHotKey, but that doesn't work with the mouse buttons. Your only solution is to create a hook with SetWindowsHookEx & that's always a huge mess. I hate to say it but in this case polling with GetAsyncKeyState might actually be the preferred solution..

1
2
3
4
5
6
7
8
9
10
11
12
while (TRUE)
{
  while (!GetAsyncKeyState(VK_LBUTTON))
    Sleep (1000); // Decrease this to increase accuracy

  // TODO: Handle the left mouse button being pushed down

  while (!GetAsyncKeyState(VK_LBUTTON))
    Sleep(10);

  // TODO: Handle the left mouse button being released
}
Awlright, thanks!
Topic archived. No new replies allowed.