Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

c++ - I'm currently making an Autoclicker and so far it's been a semi success. I need help introducing a toggle key

At the moment I'm trying to add a toggle key and make it hold to click, so that when I toggle it and hold down left click, it starts clicking. Currently it boots up and when I center the CPS it clicks, but it doesn't stop. It'll click continuously.

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

using namespace std;


int x = 0, y = 0, cps;
bool click = false;

void Menu()
{
    cout << "Add CPS (click per second):" << endl;
    cin >> cps;
}
void Clicker()
{
    while (1)
    {
        if (GetAsyncKeyState(VK_LBUTTON)) 
        {
            click = true;
        }

        if (GetAsyncKeyState(VK_RBUTTON)) 
        {
            click = false;
        }

        if (click == true)
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
            Sleep(1000 / cps);
        }
    }
}
int main()
{
    Menu();
    Clicker();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please check the following code to see if it helps:

void Clicker()
{
    while (1)
    {
        if (GetAsyncKeyState(VK_LBUTTON) & 0x8000 && !click) //Capture that auto click start condition.
        {
            click = true;
        }
        else
        {
            click = false;
        }

        while (click)
        {
            if (GetAsyncKeyState(VK_RBUTTON) & 0x8000) //Capture the stop condition.
            {
                break;
            }
            mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
            Sleep(1000 / cps);
        }
    }
}

GetAsyncKeyState Return Value:

If the most significant bit is set, the key is down.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...