M BUZZ CRAZE NEWS
// news

How do write a toggleable hold down key in AHK?

By Daniel Rodriguez

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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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