[英]Autohotkey: end a loop by pressing a certain shortcut
I searched a lot on the site but did not find how to do it. 我在该网站上进行了很多搜索,但没有找到具体方法。 This is my current script: 这是我当前的脚本:
MButton::
MouseGetPos, xpos, ypos
Sleep, 50
Click
Loop
{
Sleep 50
Send, {\ down}
Sleep 50
Send, {\ up}
}
Click %xpos%, %ypos%, 0
Return
I would like to end the loop by pressing the middle button of the mouse (MButton). 我想通过按下鼠标的中间按钮(MButton)结束循环。 I think is not a difficult task but cannot find it out, can you help thanks very much 我认为这不是一项艰巨的任务,但无法找到答案,您能不能帮助您,非常感谢
EDIT so the code posted by @Jim U, and @user3419297 works very well! 编辑,以便@Jim U和@ user3419297发布的代码效果很好! Code by GroggyOtter gives error when I run it. GroggyOtter编写的代码在我运行时给出了错误。 Code by Forivin seems to work in another way (explained below). Forivin的代码似乎以另一种方式工作(如下所述)。 Thank you so much to all of you! 非常感谢大家!
EDIT 2 an even more easy code 编辑2更简单的代码
MButton::
MouseGetPos, xpos, ypos
Sleep, 50
Click
Loop
{
Sleep 50
Send, {\ down}
Sleep 50
Send, {\ up}
If GetKeyState("MButton", "P") AND (A_TimeSinceThisHotkey > 300) ; When the MButton is pressed and after 300ms have elapsed(to prevent it from stopping direcly after triggering it.
Break
}
Click %xpos%, %ypos%, 0
Return
This starts and interrupts a loop when MButton is pressed 当按下MButton时,它将启动并中断循环
#MaxThreadsPerHotkey 2
MButton::mbutton_pressed()
mbutton_pressed()
{
global running := !running
if running
run()
}
run()
{
global running
MouseGetPos, xpos, ypos
Sleep, 50
Click
while running
{
Sleep 50
Send, {\ down}
Sleep 50
Send, {\ up}
}
Click %xpos%, %ypos%, 0
}
#MaxThreadsPerHotkey 2
allows us to invoke MButton even if the previous invocation of MButton hasn't completed. #MaxThreadsPerHotkey 2
允许我们调用MButton,即使之前的MButton调用尚未完成。 This allows us to use MButton to both start and interrupt the loop. 这使我们可以使用MButton来启动和中断循环。
All it should take is adding a toggle. 它需要做的就是添加一个切换。 Use a variable to track on and off status and use the command SetTimer
to control your loop. 使用变量来跟踪打开和关闭状态,并使用命令SetTimer
来控制循环。
; Set to 0 by default so when we press the button the first time
; it executes the if statement first.
toggle := 0
return
MButton::
toggle := !toggle ; This is the variable that tracks on/off for your loop.
; 1 becomes 0. 0 Becomes 1. It's updated as soon as the hotkey fires.
if (toggle = 1){ ; If toggle is 1, do this stuff.
MouseGetPos, xpos, ypos
Sleep, 50
Click
SetTimer, BackslashLoop, 50 ; Runs the BackslashLoop label over and over until it's turned off
; To turn it off, press MButton again.
}else{ ; Only do this stuff after MButton has been clicked again and toggle has been changed.
SetTimer, BackslashLoop, Off
Click %xpos%, %ypos%, 0
}
return
BackslashLoop:
Send, {\ down}
Sleep 50
Send, {\ up}
return
If this fixed your problem, please mark this answered. 如果这解决了您的问题,请标记为已回答。 If not, let us know what isn't working so we can figure out what's up. 如果没有,请让我们知道什么不起作用,以便我们找出问题所在。
A simple solution would be this: 一个简单的解决方案是:
#If !mbuttonIsRunning ;Only enable this hotkey when it is not running
MButton Up:: ;When the MButton is pressed
mbuttonIsRunning := True
MouseGetPos, xpos, ypos
Sleep, 50
Click
Loop
{
Sleep 50
Send, {\ down}
Sleep 50
Send, {\ up}
If GetKeyState("MButton", "P") ;If MButton is pressed
Break ;Break out of the loop
}
Click %xpos%, %ypos%, 0
mbuttonIsRunning := False
Return
#If
Another approach (based on Forivin's code): 另一种方法(基于Forivin的代码):
; autoexecute-section (top of the script):
loop_enabled := false ; the loop is disabled by default
; Press MButton down to enable the loop
MButton:: loop_enabled := true
#If (loop_enabled) ; If you enable the loop by pressing MButton down
MButton Up:: ; release MButton to start the loop
MouseGetPos, xpos, ypos
Sleep, 50
Click
Loop
{
Sleep 50
Send, {\ down}
Sleep 50
Send, {\ up}
If GetKeyState("MButton", "P") ; by pressing MButton while the loop is enabled
{
loop_enabled := false ; disable and
break ; terminate the loop
}
}
Click %xpos%, %ypos%, 0
Return
#If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.