5
I want an AutoHotKey script that waits for a particular window and then sends keystrokes to that window. However, since the waiting is done in a loop, the keys are sent again and again.
Let's say I want to wait for Windows Calculator and then send "12345" to it. My first attempt was:
#SingleInstance force
Loop
{
WinWaitActive, Calc
{
Send, 12345
}
This script obviously sends "12345" over and over again since I'm not breaking out of the loop.
If I insert a "break" following the send statement, the loop terminates but so does the entire script.
What's the standard pattern for handling this?
What exactly is the loop for? Shouldn't WinWaitActive have an internal loop that pauses script execution until the condition set forth by the function becomes true, in which case it carries out the commands in the parenthesis? – MaQleod – 2012-08-14T22:43:45.640
I put the loop to prevent the script from terminating. Perhaps my lack of understanding is how to structure AutoHotKey scripts (so they they don't terminate for example) rather than how to do what I'm trying to do here. How do I keep the script executing so it keeps doing it's thing? – Robert – 2012-08-14T22:49:54.013
The loop will keep it executing just fine, and if you are using that sort of construct, then you need some sort of conditional within the loop to only run through the WinWaitActive function if calc is not already active. This way, if it is active, it will just bypass that function call repeatedly until you click off of calc, then it will reactivate the script to wait until calc is active again. – MaQleod – 2012-08-14T22:54:56.157