Autohotkey:通过按某个快捷键结束循环
Autohotkey: end a loop by pressing a certain shortcut
我在网站上搜索了很多,但没有找到如何操作。
这是我当前的脚本:
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 的代码似乎以另一种方式工作(在下面解释)。
非常感谢大家!
编辑 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
如果这解决了您的问题,请将此标记为已回答。
如果没有,请告诉我们有什么问题,以便我们找出问题所在。
一个简单的解决方案是这样的:
#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
另一种方法(基于 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
我在网站上搜索了很多,但没有找到如何操作。 这是我当前的脚本:
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 的代码似乎以另一种方式工作(在下面解释)。 非常感谢大家!
编辑 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
如果这解决了您的问题,请将此标记为已回答。 如果没有,请告诉我们有什么问题,以便我们找出问题所在。
一个简单的解决方案是这样的:
#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
另一种方法(基于 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