exro
exro

Reputation: 50

Check if left mouse button is held down?

So I am just getting myself into coding using C++ and after learning a bit I started coding an auto clicker. I have already made it so you can toggle the auto clicker using a button on your keyboard or something of that sort, but that isn't what I'm looking for.

The problem I'm having is that I can't seem to find a way to check if the left mouse button is held down. If it is held down, then keep on clicking until the left mouse button is no longer held.

Also, I have looked around a lot to see how to do this, and I have gotten a few different things I should do, but none of them work. I was told to use:

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

from this answer here, but I also found that people were told to use:

if(GetKeyState(VK_LBUTTON) & 0x8000

Sadly I can't find where I found this in my history.

  1. I have tried getting the key state of WM_LBUTTONDOWN but it doesn't seem to pick up the mouse button is being pressed.

  2. I can get it working by checking the key state of VK_LBUTTON, but that only checks to see if the left mouse button has been pressed, not held. So it just continuously clicks until you break the while loop or close the program.

  3. Not really worth putting this down but my 12 am self, thought that checking if the left button had been pressed (like the one before), it would set a boolean value to true and then keep on clicking. But after that, I couldn't get it to stop like before. Now looking back at the code, I understand why it wasn't working.

while (1) {
   if(GetKeyState(WM_LBUTTONDOWN)) {

      Sleep(delay);
      mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
      mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
      std::cout << "Clicked" << endl;
   }

   if (GetKeyState(VK_ESCAPE)) {

      break;
   }

}

Like I said before, I have tried all different combinations to try and get this to work. But I can't see if the left mouse button is held down. I hope someone has the answer and can help me out and other people out. Anyways, thanks and have a nice day.

Upvotes: 1

Views: 6120

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595402

GetKeyState() takes a virtual key code as input, but WM_LBUTTONDOWN is not a virtual key code, it is a window message. Use VK_LBUTTON instead.

Also, GetKeyState() relies on a state machine that is local to the calling thread. That state machine is updated only when the thread processes keyboard/mouse window messages from its message queue. Your code has no such message processing. For what you are attempting, use GetAsyncKeyState() instead.

Also, mouse_event() is deprecated and has been for a long time. Use SendInput() instead.

Try this:

while (GetAsyncKeyState(VK_ESCAPE) >= 0)
{
   if (GetAsyncKeyState(VK_LBUTTON) < 0)
   {
      Sleep(delay);

      INPUT input[2] = {};

      input[0].type = INPUT_MOUSE;
      input[0].mi.dx = x;
      input[0].mi.dy = y;
      input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;

      input[1] = input[0];
      input[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;

      SendInput(2, input, sizeof(INPUT));

      std::cout << "Clicked" << endl;
   }
}

Upvotes: 1

mommos
mommos

Reputation: 189

I'm not sure whether I understood the q correctly .. but for checking the pressed state you have always to check the lowest bit in the returned high-byte of GetKeyState(VK_LBUTTON) (see GetKeyState() in MSDN-- means:

bool leftButtonPressed = 0 != (GetKeyState(VK_LBUTTON) & 0x800);

WM_LBUTTONDOWN is the message that is send to a window reporting a Left-button-down-event in the client area -- so using that in the GetKeyState() function is wrong. Try

const int delay = 50;
while (1) 
{
  if (GetKeyState(VK_LBUTTON) & 0x800)
  {
    Sleep(delay);
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, 0,0, 0, 0);
    std::cout << "Clicked" << std::endl;
  }

  if (GetKeyState(VK_ESCAPE) & 0x800)
  {
    break;
  }
}

Upvotes: 1

Related Questions