Rapid Fire - With Toggle
The rapid fire or "spamming" script is one of the most commonly requested scripts on the AutoHotkey help forum. While active, it simulates rapidly hitting a key or sequence of keys until you deactivate it. It is most often used in video games. This particular version uses a toggle, whereby a single key press both starts and stops the script.
#maxthreadsperhotkey 2 ;*1 +space:: ;2 { toggle:=!toggle ;*3 while, toggle ;*4 { send, ABC{space} ;*5 sleep, 500 ;*6 } } return end::exitapp ;*7 /* 1 Allows the hotkey to interrupt itself. This is needed for the toggle switch effect. 2 Shift+Space bar hotkey. 3 Declares a variable named "toggle", and uses the logical not "!" to set the variable to be NOT what it was before this line. Creates a toggle switch ON OFF effect. 4 A while loop. Checks the toggle variable and runs the loop if it evaluates to TRUE. 5 This can be whatever code you want to run repeatedly. In this case it sends ABC followed by a single space. 6 This sleep command sets a 500ms (one half second) pause between loops. 7 An emergency shutoff hotkey. A good idea in case you need to stop a script fast. */ |
To test, run this script then open a text editor such as Notepad. Make sure the Notepad window has focus, then press shift+space bar, then again to stop. This is only one of several methods to create a rapid fire effect. It just happens to be the method I think is most elegant.