Autoclicker at random points in different regions

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
nhiet
Posts: 1
Joined: 30 Mar 2024, 04:22

Autoclicker at random points in different regions

30 Mar 2024, 05:12

Hello. I'm new to AHK, and I'm eager to learn more.

I want to make an autoclicker to click at some points on screen. I looked around for some example, and I came across this interesting script on Reddit for autoclicker at random regions with random intervals. It's can do much better work than what I first wanted to do. And from this idea, I modified a bit so that I can click a random point in region 1 ⟶ wait a random time ⟶ click a random point in region 2 ⟶ ... I can add many regions as I want, but I have to manually specify their coordinates. Here's my script:

Code: Select all

#Requires AutoHotkey v2.0

CoordMode "Mouse"
SecondsWaitMin := 0.5
SecondsWaitMax := 0.6

^Backspace::{
    static Clicker
    if !IsSet(Clicker) { ; initialize Clicker if it's not set.
        ; Create instance of SequentialRegionClicker with random wait time
        Clicker := SequentialRegionClicker(SecondsWaitMin * 1000, SecondsWaitMax * 1000)
        ; Add regions. Coordinates are relative to the current CoordMode of Mouse.
        Clicker.AddRegion(2050, 500, 2060, 510)
        Clicker.AddRegion(2050, 1300, 2060, 1310)
    }
    ; Toggle the clicker.
    ; The Clicker will wait a random amount of time between indicated values
    ; A region will be clicked in sequence.
    Clicker.Toggle()
}

class SequentialRegionClicker {
    /**
     * @param waitMin minimum wait time
     * @param waitMax maximum wait time
     */
    __New(waitMin, waitMax) {
        this.waitMin := waitMin
        this.waitMax := waitMax
        this.ClickerFunc := ObjBindMethod(this, "Clicker")
        this.ClickerState := false
        this.Regions := []
        this.CurrentRegion := 1
    }

    /**
     * Adds a new region to click.
     * @param x1 x-position of top left corner
     * @param y1 y-position of top left corner
     * @param x2 x-position of bottom right corner
     * @param y2 y-position of bottom right corner
     */
    AddRegion(x1, y1, x2, y2) {
        ; Push an array of coordinates to this.Regions.
        This.Regions.Push([x1, y1, x2, y2])
    }

    /**
     * Start the clicker at a random time between this.waitMin and this.waitMax.
     */
    Start() {
        this.ClickerState := true
        this.Clicker() ; Call clicker.
        SetTimer this.ClickerFunc, -Random(this.waitMin, this.waitMax) ; After a random amount of time, Clicker is called.
    }

    /**
     * Stop the clicker.
     */
    Stop() {
        this.ClickerState := false
        SetTimer this.ClickerFunc, 0
    }

    /**
     * Toggle the clicker.
     */
    Toggle() {
        (this.ClickerState = true) ? this.Stop() : this.Start()
    }

    /**
     * This Clicker method is called by SetTimer.
     */
    Clicker() {
        ; Pick the current region from this.Regions.
        CurrentRegion := this.Regions[this.CurrentRegion]
        ; Pick a random x-position from region.
        RandomX := Random(CurrentRegion[1], CurrentRegion[3])
        ; Pick a random y-position from region.
        RandomY := Random(CurrentRegion[2], CurrentRegion[4])
        ; Click the random position.
        Click RandomX, RandomY
        ; Run the timer again at a random time between this.waitMin and this.waitMax.
        SetTimer this.ClickerFunc, -Random(this.waitMin, this.waitMax)
        ; Move to the next region, or loop back to the first region if we're at the end.
        this.CurrentRegion := (this.CurrentRegion < this.Regions.Length) ? (this.CurrentRegion + 1) : 1
    }
}
Now I want to upgrade this script so that
1. When I press the toggle hotkey for the code, a tooltip will appear next to my cursor "Select region 1"
2. I can move my cursor around, the tooltip will follow it
3. When I click at a point, says (500, 500), it will create a region around it with defined size, for example RegionSize := 10 will result in (495, 495, 505, 505)
4. Then another tooltip will appear "Select region 2"
5. I can keep clicking to select regions until I have clicked enough number of regions that I defined in the script, for example RegionNumber := 2

For the tooltip, I'm thinking of MouseGetPos, but I don't know how to constantly feed those coordinates to ToolTip so that it will follow my cursor around.

For the regions, this is my idea for its size

Code: Select all

MouseGetPos &xpos, &ypos
x1 := xpos - RegionSize / 2
y1 := ypos - RegionSize / 2
x2 := xpos + RegionSize / 2
y2 := ypos + RegionSize / 2
this.AddRegion(x1, y1, x2, y2)
but then I don't know how to make it keep adding regions until it reaches the RegionNumber

I would really appreciate your help. Thank you!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada, Rohwedder and 26 guests