7

Using AutoHotKey, I have a rather simple loop script that I want to be able to break by the stroke of a key. I tried a few different codes from websites but it doesn't seem to work.

Here's the code:

#g::
Loop 20
{
    MouseClick, left,  142,  542
    Sleep, 1000
    MouseClick, left,  138,  567
    Sleep, 1500
    MouseClick, left,  97,  538 
    Sleep, 1000
}
pb2q
  • 58,613
  • 19
  • 146
  • 147
Dave
  • 71
  • 1
  • 2

4 Answers4

2

Use a global variable (keepCycling) and toggle it to break the loop. Global variables should be declared in the beginning of a script.

1

Adding a global variable might be the easiest solution for your case since your loop takes a while to complete.

global break_g = 0 

#b::
    break_g = 1 
return


#g::
break_g = 0
Loop 20
{
    MouseClick, left,  142,  542
    Sleep, 1000
    MouseClick, left,  138,  567
    Sleep, 1500
    MouseClick, left,  97,  538 
    Sleep, 1000
    if( break_g = 1)
    {
        return
    }
}
return ; also you were missing this return 
0
#g::  
Loop 20  
{  
    KeyWait,Ctrl,D T0  
        if Errorlevel = 0  
            break  
    MouseClick, left,  142,  542  
    KeyWait,Ctrl,D T0  
        if Errorlevel = 0  
            break  
    Sleep, 1000  
    KeyWait,Ctrl,D T0  
        if Errorlevel = 0  
            break  
    MouseClick, left,  138,  567  
    KeyWait,Ctrl,D T0  
        if Errorlevel = 0  
            break  
    Sleep, 1500  
    KeyWait,Ctrl,D T0  
        if Errorlevel = 0  
            break  
    MouseClick, left,  97,  538  
    KeyWait,Ctrl,D T0  
        if Errorlevel = 0  
            break  
    Sleep, 1000  
}  

return

Using the above can be helpful as the effect would be instantaneous. More often than not, you will have your loop stopped when you hold Ctrl for an interval.

Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
Avi
  • 1,341
  • 1
  • 18
  • 28
  • You have flaws in your code. You have to hold ctrl for a the full duration of the sleep time if you start holding it when sleep is executed. So effect is far from instant. You also duplicated same command six-times, you should not repeat yourself (what if OP wants to change the hotkey, good luck doing that 6+ times). And you don't know if the whole loop is a single command that should not be interrupted; using my method you can still put an if statement(if you want, that is up to the user) whenever you want and achieve a termination of the loop. –  Apr 25 '13 at 12:31
  • @armin , I know that Sleep fact.(So I have used "more often than not") Also my answer was mostly question-specific and not for the general situation. (As dave's script is small, i can afford the repetition 6 times ). and there is no need for that **break_g** being a "global" . I see you edited that point from my code. – Avi Apr 26 '13 at 08:48
0

Toggling global variable is the way to go. You need to declare it in the beginning of the script.

global keep_working = 1 ; set break to off in the beginning of the script

b:: ; set break on keep_working = 0 return

g:: ; set working to on and start the loop keep_working = 1
Loop, ; loop until b is pressed (was loop, 20 in original code) { MouseClick, left, 142, 542 Sleep, 1000 MouseClick, left, 138, 567 Sleep, 1500 MouseClick, left, 97, 538 Sleep, 1000 if( keep_working = 0) { return ; required to stop execution } } return ; this delimiter is required at the end of hotkey procedure.

sixtytrees
  • 1,156
  • 1
  • 10
  • 25