-1
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

int c;

void menu() {
    cout << "Enter CPS: ";
    cin >> c;
    cout << "Autoclicker will activate when left mouse button is held down." << endl;
}

void mod() {
    bool autoclickerActive = false;

    while (true) {
        if (GetAsyncKeyState(VK_LBUTTON) & 0x8000) {
            if (!autoclickerActive) {
                cout << "Autoclicker activated!" << endl;
                autoclickerActive = true;
            }
            
            int lower = c - 6, upper = c + 10;
            int random = lower + rand() % (upper - lower + 1);
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            Sleep(1000 / random);
        } else {
            if (autoclickerActive) {
                cout << "Autoclicker deactivated." << endl;
                autoclickerActive = false;
            }
        }
        Sleep(10);
    }
}

int main() {
    srand(time(0));

    menu();
    mod();

    return 0;
}

I'm trying to make it so that the autoclicker continuously clicks the left mouse button while the left mouse button is held down. The issue I'm facing is that the loop cancels itself after the 1st iteration because it sends leftup. So this program instantly stops when i press the left mouse button and hold it down. If I make it so that the autoclicker activates when i hold down the right mouse button, it works completely fine.

Is there a way to make the auto clicker click the left mouse button repeatedly while the user is holding down the left mouse button?

8
  • First, I'd use SendInput instead of the old mouse_event API - Secondly: it may be easier to get it working correctly if you use a different trigger for it, using an unused button to do the repeated clicks.
    – Ted Lyngmo
    Commented Jul 7 at 19:46
  • I'll look into SendInput. Yeah I know I can use a different button for the trigger but I need it to be the left mouse button as I mentioned in the question. Thanks tho!
    – Neal
    Commented Jul 7 at 20:14
  • Ok, I got it working (using SendInput) by sending key-up + key-down (in one SendInput message) instead of the other way around. example
    – Ted Lyngmo
    Commented Jul 7 at 20:56
  • 1
    omg im actually so dumb. Yeah with the code I mentioned above in the question I just made it release the mouse button first then hold down the mouse button. That just fixed the whole issue. Thanks alot man!
    – Neal
    Commented Jul 7 at 21:07
  • :-) You're welcome! Glad it helped!
    – Ted Lyngmo
    Commented Jul 7 at 21:10

0

Browse other questions tagged or ask your own question.