Automating keystrokes is a fun way to spend an afternoon. It’s also an entertaining to wrap your head around the idea of loops and variables. If you’re wanting extra practice, give these a try.
Note: You’ll need AutoHotkey installed for this. If you don’t know what that is, click here.
Important Commands to Note:
run, Name of Program.exe
SendInput Keystrokes to Send
Sleep, Delay in Milliseconds
Loop, # of times to run loop
{
Commands to run
}
All Work and No Play
Take a look at this code.
run, notepad.exe
Loop, 100
{
SendInput All Work And No Play Makes Jack A Dull Boy.
}
return
Keep in mind that everything inside the loop will happen 100 times, so with just 6 lines of code, you can make the computer type this sentence over and over again. In action, it kind of makes your computer seem possessed.
A Little Slower Please
Timing is key when automation gets more complicated. Programs need a little time to start up, so commands sent instantaneously can be missed.
That’s where Sleep comes in handy.
This code has the computer start Notepad, wait a bit, send the ALT key to get into Format and then Font, and then finally type out a font name before getting to the loop.
run, notepad.exe
Sleep, 1000
SendInput {Alt}
Sleep, 500
SendInput O
Sleep, 500
SendInput F
Sleep, 500
SendInput Showcard Gothic {Enter}
Sleep, 1000
Loop, 10
{
SendInput All Work And No Play Makes Jack A Dull Boy. {enter}
Sleep, 500
}
return
The final product may still be too fast for you to see the keystrokes changing the font type.
Now Make It Unravel
This code uses a variable in a loop to calculate the number of times the computer should send a tab. At the end of the outer loop, the variable is increased by one so that every time it runs, it adds another tab after “All Work and No Play…”
run, notepad.exe
Sleep, 1000
SendInput {Alt}
Sleep, 500
SendInput o
Sleep, 500
SendInput f
Sleep, 500
SendInput Showcard gothic {Enter}
Sleep, 1000var = 1
Loop, 50
{
SendInput All Work And No Play Makes Jack A Dull Boy. {enter}
Loop, 50
{
if (a_index <= var)
{
SendInput {Tab}
} else {
continue
}
}
var+=1
}
return
As the variable increases, the tabs push the sentences further and further apart. The result is this: