I want to create an AutoHotKey script that waits for a window and sends some keystrokes to it, but the keystrokes are repeating by default

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?

Robert

Posted 2012-08-14T22:18:21.713

Reputation: 53

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

Answers

4

I'm assuming you want to stop sending 12345 until you activate the window again (or another window with the same name). So use WinWaitNotActive

#SingleInstance force
Loop
{
WinWaitActive, Calc
{
    Send, 12345
    WinWaitNotActive, Calc
}

Der Hochstapler

Posted 2012-08-14T22:18:21.713

Reputation: 77 228

Bingo. So once the window becomes active and the keys are sent, script execution pauses on the WinWaitNotActive... line? – Robert – 2012-08-15T02:34:19.460

@Robert yes it will pause at that line. – avirk – 2012-08-15T03:12:11.120