简体   繁体   中英

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). 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! Code by GroggyOtter gives error when I run it. Code by Forivin seems to work in another way (explained below). Thank you so much to all of you!

EDIT 2 an even more easy code

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

#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. This allows us to use MButton to both start and interrupt the loop.

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.

; 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):

; 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM