How Do Write a Toggleable Hold Down Key in Ahk?

I'm trying to write an AHK script that allows me to do the following:

Press the t key to toggle the 1 key being held down so that it types a bunch of 1's indefinitely until I press the t key again.

Just to be clear: I want it to repeatedly send the 1 key input and not send the command to hold the 1 key down without typing anything.

I'm aware that this could be done with a loop but unsure of how to do it; Although the t key should start and stop the loop. I also don't want the loop to be affected (even if I pressed other keys) unless it is by me manually stopping it using the t key.

2 Answers

If I understand right, you want the key t to start and stop the repeating of the key 1, acting as a switch.

Below is a script that does this, repeating the key at intervals of 200 milliseconds:

#Persistent
repeating1 = 0

t::
if (repeating1 = 0)
{
    repeating1 = 1
    SetTimer, repeat1, 200
}
else
{
    repeating1 = 0
    SetTimer, repeat1, Off
}
return

repeat1:
send, 1
return
0

There you go

#Persistent
#MaxThreadsPerHotkey 2
toggle := False

z UP::
toggle := !toggle

Loop {
    If (!toggle) {
    send,{z UP}
        break
    }
    send,{z DOWN}
sleep 10
}
Return
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest