collin
collin

Reputation: 3

auto hot key script, 2 functions with 1 press

I currently have an auto clicker where when I hold the left mouse button it spam clicks at whatever speed I want. I want to add another key press to the same script.

I want the mouse to keep spamming fast, and then I want 'e' to be spammed every 1 second while holding down the mouse 1 key

This is my current auto clicker that works

F1::
Suspend Toggle
Return

~$LButton::
While GetKeyState("LButton","P"){
    Click Left   
    Sleep 5

}
return

I thought if I just added another line it would work, like this, but it doesnt. It makes sense in my head, but I dont know how to code :( and Ive been searching the auto hot key forums all night.

F1::
Suspend Toggle
Return

~$LButton::
While GetKeyState("LButton","P"){
    Click Left   
    Sleep 5
    send e
    sleep 1000
}
return

my goal is that I want the mouse to keep spamming fast, and then I want 'e' to be spammed every 1 second while holding down the mouse 1 key

Upvotes: 0

Views: 2282

Answers (2)

PGilm
PGilm

Reputation: 2312

You can't run two "Sleeps" like that. You will end up waiting 1 second after the send e before getting back to your Click Left. Use a counter and some math. And, since 5 ms may be too quick for AHK to even process, try using 20 (which may even still be too fast -- you want the biggest number that will still allow the quickest mouse clicks).

Try:

~$LButton::
While GetKeyState("LButton","P"){
    Click Left   
    i++
    IfEqual, i, 50
    {
        send e
        i=
    }
    Sleep 20
}
return

And let us know if that works for you . . .

Upvotes: 1

MCL
MCL

Reputation: 4065

Here's a solution using timers. Timers have the advantage that they don't block the current thread and thereby allow subsequent code in the same thread to be executed quasi-synchronously.

~$LButton::
    SetTimer, LeftClick, 50
    SetTimer, SendEKey, 1000
return

~$LButton up::
    SetTimer, LeftClick, Off
    SetTimer, SendEKey, Off
return

LeftClick:
    Click, Left
return

SendEKey:
    Send, e
return

One remark:

Clicking every 5ms seems a bit too much to me. Depending on your script settings, you won't reach this small delay anyway. If you're not using SendMode Input, the default Key Delay will be 10ms per keypress. Additionally, if SetBatchLines isn't configured, your script will wait 10ms per executed line. I recommend playing around with these parameters in order to find the biggest possible delay that still clicks fast enough. This will save some resources and minimize the risk of some kind of overflow situation.

Upvotes: 2

Related Questions