2017-06-13 266 views
0

我在網站上搜索了很多,但沒有找到如何去做。 這是我當前的腳本: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 

回答

0

這將啓動,並中斷當按下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來啓動和中斷循環。

+0

太感謝你了,它的工作原理! –

0

所有它應該採取的是添加一個切換。使用變量來跟蹤開啓和關閉狀態,並使用命令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 

如果這能解決您的問題,請標記爲已回答。 如果沒有,讓我們知道什麼是不工作,所以我們可以弄清楚發生了什麼。

+0

'SetTimer,toggleLabel,Off'應該是'SetTimer,BackslashLoop,Off'。 – user3419297

+0

非常感謝你,但是當我運行它時,出現這個錯誤http://i.imgur.com/nl3HIVO.png –

+0

這是剩餘的代碼。我使用熱鍵來切換,而toggleLabel是我使用的默認變量。像user3419297所說的那樣,它應該是BackslashLoop。當我寫這篇文章時,我錯過了更改:P Post已更新。編輯:[所以你可以看到我的意思:P](http://i.imgur.com/UxdKgrt.gifv) – GroggyOtter

0

簡單的解決辦法是這樣的:

#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 
+0

非常感謝你,但是這有效的另一種方式:當我舉行Mbutton腳本暫停,當我釋放Mbutton時,腳本會恢復。 Mbutton看起來像一個暫停按鈕。 –

0

另一種方法(基於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