0

How do I make my auto clicker randomize the ms it clicks at? I'm asking this question because I could not find any articles on this and I've been needing to get this question answered for a very long time but could not find any articles on it as I said.

Please provide me the code.

My code:

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

using namespace std;

int main()
{

    cout << "\"F\" = Toggle on\n";
    cout << "\"R\" = Toggle off\n\n";

    bool Click = false;

    while (1)
    {
        if (GetAsyncKeyState('F')) // Toggle on
        {
            Click = true;
        }

        if (GetAsyncKeyState('R')) // Toggle off
        {
            Click = false;
        }

        if (Click == true) // Autoclicker
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            Sleep(50);
        }
    }
    return 0;
}

I want it to click 50 to 60 ms

  • https://stackoverflow.com/questions/13445688/how-to-generate-a-random-number-in-c – Tas Dec 13 '18 at 01:58
  • Randomize the parameter of `Sleep()` – Andreas Dec 13 '18 at 01:59
  • https://learn.microsoft.com/en-us/windows/desktop/api/timeapi/nf-timeapi-timebeginperiod – Hans Passant Dec 13 '18 at 02:04
  • A little more info will allow us to be helpful. For instance, do you plan a 'short' click every 50 to 60 ms intervals? or "on-delay1-off-delay2", where delayX is one duration of 11 possible evenly distributed values, in range 50 to 60 ms? geiger counter style clicks? metronome / rythmic clicks? Perhaps your auto-clicker is suggesting something that soulds like a mouse click? – 2785528 Dec 13 '18 at 02:17
  • I found a way to do it, thanks for your help. – Hardware Broz Dec 29 '18 at 22:01

1 Answers1

0

Utilizing the chrono library we can setup a timer, to measure the delay between key presses and we can use the randomize library to randomize the delay.

For toggles, use &1 in your GetAsyncKeyState() call to test if the least significant bit is set.

Here is a good solution:

#include <windows.h>
#include <iostream>
#include <chrono>
#include <thread>
#include <random>

int main()
{
    INPUT input{ 0 };
    input.type = INPUT_MOUSE;

    bool bClick = false;

    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(1, 300);
    int random_value = distribution(generator);

    using Clock = std::chrono::steady_clock;
    std::chrono::time_point<std::chrono::steady_clock> start, now;
    std::chrono::milliseconds duration;

    start = Clock::now();

    while (true)
    {
        //toggles it on and off with one key
        if (GetAsyncKeyState('U') & 1)
            bClick = !bClick;

        if (bClick)
        {
            now = Clock::now();
            duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - start);
                
            if (duration.count() >= random_value)
            {
                int random_value = distribution(generator);
                std::cout << random_value << std::endl;

                input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
                SendInput(1, &input, sizeof(INPUT));


                input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
                SendInput(1, &input, sizeof(INPUT));
                start = std::chrono::steady_clock::now();
            }
        }
    }

    return 0;
}
GuidedHacking
  • 3,628
  • 1
  • 9
  • 59