Home > Software engineering >  Mouse click with GetAsyncStateKey
Mouse click with GetAsyncStateKey

Time:01-24

I am trying to make a program that when you click, it uses GetAsyncStateKey() to know that you've clicked and makes the code click once more, like a double-clicker.

The problem is, I don't know anything about coding, so I tried to look at many codes and came up with this:

#include <iostream>
#include <windows.h>
#include <fstream>

using namespace std;

int main()
{
    if(GetAsyncKeyState(VK_LBUTTON)) {
       mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
       Sleep(0);

       return 0;
    }
}

In my mind, when it opens, it should stay open, hence the sleep, then getting the part if I click, it clicks again. But in reality, when I open it, it closes immediately.

Can someone make it work for me?

CodePudding user response:

Your program performs 1 operation and then exits. You need a loop to keep it alive, eg:

#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    while (true) {
        if (GetAsyncKeyState(VK_LBUTTON) < 0) {
           mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }
        Sleep(0);
    }
    return 0;
}
  •  Tags:  
  • Related