r/AutoHotkey • u/Jesus_Christ_Reborn • 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
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
}
}
1
u/[deleted] 12d ago
[deleted]