Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
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 Aug 14 '12 at 22:43
    
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 Aug 14 '12 at 22:49
    
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 Aug 14 '12 at 22:54

1 Answer 1

up vote 4 down vote accepted

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
}
share|improve this answer
    
Bingo. So once the window becomes active and the keys are sent, script execution pauses on the WinWaitNotActive... line? –  Robert Aug 15 '12 at 2:34
    
@Robert yes it will pause at that line. –  avirk Aug 15 '12 at 3:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.