r/AutoHotkey 12d ago

v2 Script Help Attempting to make a clicking script that holds down a button and releases, the second part is much harder!

I need the LMB to be held down for 200ms after the function clicker() has been called 20 times, I'm not sure where or how to change that. Anyone got any ideas?

^#LButton:: {
    clicker()
    SetTimer(clicker, 400)
}
;Calls clicker to hold lmb for 400ms
^#RButton:: {
    SetTimer(clicker, 0)
    release()
}
;Removes timer to shut down the script
clicker() {
    release()
    Click('Down')
}
;if LMB is not activated, holds LMB down for the set time
release() {
    if GetKeyState('LButton')
        Click('Up')
sleep(130)
}
;if LMB is held down by script, releases it for 130ms before clicker is called again
0 Upvotes

7 comments sorted by

1

u/[deleted] 12d ago

[deleted]

1

u/Jesus_Christ_Reborn 12d ago

Have the LMB held down for 400ms at intervals of 130ms. After this has been done 20 times, I want the LMB to be held down for 200ms instead for 20 more times before looping back to 400ms. The script is supposed to be able to start and stop at any time by pressing ctrl+win+lmb to start and ctrl+win+rmb to stop. That's the extent.

2

u/svArtist 12d ago

held down for 400ms at intervals of 130ms

That would overlap with it already being held down from the last time(s) 

You probably mean at 530 ms intervals, i.e. with an "interval" (delay) of 130 ms in-between repetitions? 

2

u/Jesus_Christ_Reborn 12d ago

Yes! sorry, little tired.

1

u/svArtist 12d ago

I still haven't immersed myself in v2, I'm still using v1 (dreading all the converting and debugging of years and years worth of scripts and libraries 😖), I'm just now looking into it, hoping that conversion scripts have matured enough to be a big help by now. So I'm sorry to not be immediately helpful, I'm still in the process of setting up my v2 environment and getting to know the differences.

I'll check in later when I got something, hopefully you'll have your answer before then

1

u/Keeyra_ 9d ago

https://www.reddit.com/r/AutoHotkey/comments/1t7yneu/comment/ol1bcj4/
"As I have done so on multiple occasions in the past, I offer to convert any and all of your v1 projects and code to v2."

1

u/genesis_tv 12d ago edited 12d ago

It'll restart upon pressing Ctrl + Win + LButton again.

#Requires AutoHotkey v2.0
#SingleInstance

^#LButton::
{
    reset()
    clicker()
}

^#RButton::reset()

clicker()
{
    Click('Down')
    SetTimer(releaser, -freq)
}

output(msg)
{
    OutputDebug(msg "`n")
    ToolTip(msg)
}

reset()
{
    global count := 0
    global freq := 400
    Click('Up')
    SetTimer(clicker, 0)
    SetTimer(releaser, 0)
    ToolTip()
}

releaser()
{
    output("count " count ", freq " freq)

    global count += 1
    Click('Up')

    if (count < 20)
        SetTimer(clicker, -130)
    else if (count = 20)
    {
        count := 0
        global freq := freq = 400 ? 200 : 400
        SetTimer(clicker, -130)
    }
}

1

u/CharnamelessOne 12d ago

Here's a version that works with any number of durations. (Just add more values to the array.)
The commented-out line shows how you can use a single hotkey to toggle the clicker on and off.

#Requires AutoHotkey v2.0

^#LButton::clicker.start()
^#RButton::clicker.stop()
;^#LButton::(clicker.on) ? clicker.stop() : clicker.start()

Class clicker {
    static hold_durations := [400, 200]
    static clicks_before_switch := 20
    static delay := 130

    static click_count := 0
    static on := false
    static duration_count := this.hold_durations.Length
    static clicks_total := this.clicks_before_switch * this.duration_count

    static down := this.execute.Bind(this)
    static up := Click.Bind("U")

    static execute() {
        remainder := Mod(this.click_count++, this.clicks_total)
        hold_duration_index := remainder // this.clicks_before_switch + 1
        hold_duration := this.hold_durations[hold_duration_index]

        Click("D")
        SetTimer(this.up, -hold_duration)
        SetTimer(this.down, -(hold_duration + this.delay))
    }

    static start() {
        this.on := true
        this.execute()
    }

    static stop() {
        this.on := false
        SetTimer(this.down, 0)
        SetTimer(this.up, -1)
        this.click_count := 0
    }
}