AutoHotKey keystroke break loop

ぐ巨炮叔叔 提交于 2019-12-10 04:21:33

问题


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
}

回答1:


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




回答2:


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 



回答3:


#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.




回答4:


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.



来源:https://stackoverflow.com/questions/16203871/autohotkey-keystroke-break-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!