Autohotkey:按一定的快捷键结束循环
我在网站上搜索了很多,但没有找到如何去做。 这是我当前的脚本:Autohotkey:按一定的快捷键结束循环
MButton::
MouseGetPos, xpos, ypos
Sleep, 50
Click
Loop
{
Sleep 50
Send, {\ down}
Sleep 50
Send, {\ up}
}
Click %xpos%, %ypos%, 0
Return
我想通过按下鼠标(MButton)的中间按钮来结束循环。 我认为不是一件困难的事情,但找不到它,你能非常感谢
编辑所以@Jim U和@ user3419297张贴的代码工作得很好! GroggyOtter的代码在运行时会出错。 Forivin的代码似乎以另一种方式工作(如下所述)。 非常感谢你们所有人!
EDIT 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
这将启动,并中断当按下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
让我们来调用MButton即使MButton以前调用尚未完成。这允许我们使用MButton来启动和中断循环。
所有它应该采取的是添加一个切换。使用变量来跟踪开启和关闭状态,并使用命令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
如果这能解决您的问题,请标记为已回答。 如果没有,让我们知道什么是不工作,所以我们可以弄清楚发生了什么。
'SetTimer,toggleLabel,Off'应该是'SetTimer,BackslashLoop,Off'。 – user3419297
非常感谢你,但是当我运行它时,出现这个错误http://i.imgur.com/nl3HIVO.png –
这是剩余的代码。我使用热键来切换,而toggleLabel是我使用的默认变量。像user3419297所说的那样,它应该是BackslashLoop。当我写这篇文章时,我错过了更改:P Post已更新。编辑:[所以你可以看到我的意思:P](http://i.imgur.com/UxdKgrt.gifv) – GroggyOtter
简单的解决办法是这样的:
#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
非常感谢你,但是这有效的另一种方式:当我举行Mbutton脚本暂停,当我释放Mbutton时,脚本会恢复。 Mbutton看起来像一个暂停按钮。 –
另一种方法(基于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
太感谢你了,它的工作原理! –