![](/img/trans.png)
[英]End continuous loop and restart script with same hotkey - 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.