Toggling key spam

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Hiruminho
Posts: 2
Joined: 22 Jun 2017, 18:03

Toggling key spam

22 Jun 2017, 18:12

Hi, i'm trying to use the "ctrl+0" hotkey to spam the "2" key 5 times a second, i'm using the following script right now:

Code: Select all

^0::
    SetTimer, PressTheKey, 200
Return

PressTheKey:
    Send, 2
Return
but whenever i press the "ctrl+0" hotkey it just starts spamming 2 like there's no tomorrow, and i wish i could stop it by using ctrl+0 again (or if it's not possible could stop with another hotkey also, it's fine), i've tried the following script:

Code: Select all

^0::pause, toggle
    SetTimer, PressTheKey, 200
Return

PressTheKey:
    Send, 2
Return
but when i press "ctrl+0" it just toggles the script on and off and don't spam the "2" key, thank you in advance for any help.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Toggling key spam

22 Jun 2017, 20:01

Check out the toggle example in (Note: The first link is from the old forums, the second link is on these forums) http://www.autohotkey.com/board/topic/6 ... re-thread/ or https://autohotkey.com/boards/viewtopic.php?f=7&t=11952

The key is using a variable you change between true and false (easily done with := and ! operators), and using If statements for turning your timer on or off.
Hiruminho
Posts: 2
Joined: 22 Jun 2017, 18:03

Re: Toggling key spam

23 Jun 2017, 02:49

Thank you! i'm using the one that spams when you hold down the button (1st example on the 2nd link you sent)

Code: Select all

setKeyDelay, 50, 50
setMouseDelay, 50

$~lbutton::
	while (getKeyState("lbutton", "P"))
	{
		send, {lbutton}
		sleep, 100
	}
return
but i don't know why i couldn't make it work with the "^0" so i just changed it to "0" it works for me as well, also, could you help me to understand what setKeyDelay, setMouseDelay and the "$" before the ~lbutton does? i couldn't understand those it all i'm (very) new in scripting and am liking it a lot! lol thank you for your help.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Toggling key spam

23 Jun 2017, 08:21

Check out the documentation for a better explanation that I could give on SetKeyDelay and SetMouseDelay. Essentially, it sets how long to wait between Key/Mouse actions, and how long to do those actions (hold down a button).

The $ prevents recursion of a hotkey. (It is usually not a problem when using a mouse button like lbutton, but it can be a problem when using a keyboard button like 0.) What I mean by recursion here is a hotkey activating itself. When you use Send, even the AHK script can capture that output and trigger a hotkey. As a consequence, it is possible you get stuck in an accidental loop of the Send key activating the key:: hotkey, whatever key may be. This is prevented with the $ modifier. (Also, the ~ modifier is a passthrough, such that the natural action of the hotkey is not blocked.)

What I had originally been thinking for you was a code like this:

Code: Select all

^0::
toggle:=!toggle ; change between true and false for the variable toggle
If toggle ; if toggle is true
    SetTimer, PressTheKey, 200
else
    SetTimer, PressTheKey, Off
Return

PressTheKey:
    Send, 2
Return

Also, I overlooked it before. In your second code example with the Pause command, the reason you were probably getting unexpected results was you made a one-line hotkey. What you did may actually have worked if you used

Code: Select all

^0::
Pause, Toggle
SetTimer, PressTheKey, 200
...
But I do say may. I'm not as familiar with Pause and how it may work with timers; and I haven't tested your code with that slight change.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 253 guests