How to trigger loop when left mouse down autoclicker c++

Viewed 1230

Hey I am trying to make my autoclicker only click when I hold left mouse button down.

void clikr()
{
  INPUT Input ={0};
  int sleepzZz = 1000 / times;
  active = false;
  system(“cls”);
  while(1)
{
    if((GetAsyncKeyState(VK_LBUTTON) & 0x8000) != 0)
    {
      Sleep(sleepzZz);
      Input.type = INPUT_MOUSE
      Input.mi.dwFlags =  MOUSEEVENTF_LEFTDOWN;
      ::SendInput(1, &Input, sizeof(INPUT))

      ::ZeroMemory(&Input, sizeof(INPUT));
      Input.type = INPUT_MOUSE;
      Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
      ::SendInput(1, &Input, sizeof(INPUT));


     }

 }

(I typed this out on my phone I’m so sorry ) Basically the loop cancels it self because it send leftup.

I understand I can bind it to another key but how would I go about getting this to work when I hold leftdown

Please help

1 Answers

Using GetAsyncKeyState or GetKeyState is bad for this case. Here is a quote from MSDN stating that exactly what you are trying to do is not suited to be done with the two functions in combination with SendInput.

This function does not reset the keyboard's current state. Any keys that are already pressed when the function is called might interfere with the events that this function generates.

VERY IMPORTANT: What you should be aiming for is to distinguish between inputs that are not simulated by your program and those that are. This does not mean you should complicate the heck out of this code and verify input to be legitimate with hooks (More info on this approach in the answers here). You just want to see if an input is generated in your loop or not in your loop.

I came up with this simmilar solution that sadly still uses the two functions because i do not know other ones that will test for input. It is not perfect but its the closest i could get with simple code.

VOID WINAPI ClickLoop()
{
    /** wait for any LMB press. */
    while ((GetAsyncKeyState(VK_LBUTTON)&0x8000) == 0)
        Sleep(1);

    INT nCurrKeyState = GetKeyState(VK_LBUTTON);
    INT nPrevKeyState;

    do
    {
        INPUT Input;
        ZeroMemory(&Input, sizeof(INPUT));
        Input.type = INPUT_MOUSE;


        Input.mi.dwFlags =  MOUSEEVENTF_LEFTUP;
        SendInput(1, &Input, sizeof(INPUT));

        Sleep(0); // how many miliseconds between the clicks.
        Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
        SendInput(1, &Input, sizeof(INPUT));


        // !! Do not set this number too low
        // !! Setting it too low might cause infinite loops
        //     and you cannot stop your clicks at first try.
        Sleep(70); // how many miliseconds the click is held.

        nPrevKeyState = nCurrKeyState;
        nCurrKeyState = GetKeyState(VK_LBUTTON);

        if ((GetAsyncKeyState(VK_LBUTTON)&0x8000) == 0)
            break;
    }while (nCurrKeyState != nPrevKeyState);
}

And in your main you can have something like:

int main()
{
    while(TRUE)
    {
        printf("In click loop...\n");
        ClickLoop();
    }

    return 0;
}

Still, this is not a proper solution but i do not know any other functions for testing keys on windows aside than GetAsyncKeyState and GetKeyState. It also has the downside that you cannot set the time between clicks less than 70~80 ms or you can break it.

Related