Search code examples
loopstoggleautohotkeyhotkeys

Toggle a key with hotkey in autohotkey


So I tried to automate running in a game, where the map is huge, and I have to run miles. I wanted to toggle on the hotkey (Ctrl+Shift+A or something else) press the running (in the game, I can run with w). I tried code, like:

Pause On
Loop
Send w
+^a::Pause

(it can press the w, but it can't release) and like this:

+^a::
toggle := !toggle

while toggle
    Send {w down}

(same problem). It's just my problem, or these codes are wrong?


Solution

  • This is my stock function. I usualy map it to ^W or Q. Pressing w or s will cancel it. Easy peasy.

    HoldW(){
        SendInput {w up}{w down}
        Loop
        {
            Sleep 100
            GetKeyState state, w
            if state = u
                return
            If GetKeyState("s")
            {
                SendInput {w up}
                return
            }
        }
    }