My loop break isn't working. Trying to use one hotkey to end another hotkey's infinite loop.

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
gaveitatry
Posts: 13
Joined: 12 Feb 2023, 17:37

My loop break isn't working. Trying to use one hotkey to end another hotkey's infinite loop.

Post by gaveitatry » 28 Mar 2023, 00:18

Code: Select all

#Requires AutoHotkey v2.0

target := 'ahk_exe notepad.exe'


#HotIf WinExist(target)

^+[:: {

BreakLoop := False

Loop {

  If (BreakLoop == True)
  Break

  Sleep 0500
  ControlSend 'doing stuff', 'Edit1'

}}


^+]:: {

SoundBeep 1000

BreakLoop := True

  Sleep 0500
  ControlSend 'done', 'Edit1'

}

#HotIf

When I press Ctrl + Shift + [, it starts typing "doing stuff" in the notepad.exe document, on a loop. And when I press Ctrl + Shift + ], I hear a beep. But it never stops the loop and it never types "done" in the notepad document.

User avatar
boiler
Posts: 16954
Joined: 21 Dec 2014, 02:44

Re: My loop break isn't working. Trying to use one hotkey to end another hotkey's infinite loop.

Post by boiler » 28 Mar 2023, 02:01

You have a variable scope problem. As you have it, BreakLoop is local to each function. Declare it as global in both.

gaveitatry
Posts: 13
Joined: 12 Feb 2023, 17:37

Re: My loop break isn't working. Trying to use one hotkey to end another hotkey's infinite loop.

Post by gaveitatry » 28 Mar 2023, 10:02

Someone showed me how it is done. The working script is as below.


Code: Select all

#Requires AutoHotkey v2.0

target := 'ahk_exe notepad.exe'

BreakLoop := False

#HotIf WinExist(target)

^+[:: {

global BreakLoop
BreakLoop := False

Loop {

  If (BreakLoop = True)
  Break

  Sleep 0500
  ControlSend 'doing stuff', 'Edit1'

}}


^+]:: {

global BreakLoop

SoundBeep 1000

BreakLoop := True

  Sleep 0500
  ControlSend 'done', 'Edit1'

}

#HotIf

gaveitatry
Posts: 13
Joined: 12 Feb 2023, 17:37

Re: My loop break isn't working. Trying to use one hotkey to end another hotkey's infinite loop.

Post by gaveitatry » 22 May 2023, 19:06

UPDATE:

I was using the script below, but it turns out that it is semi broken. Basically, I have three hotkey commands. Ctrl + Shift + [ starts an endless game suicide loop so I can can stay dead and spectate the game as a dead player. Ctrl + Shift + } is supposed to rename my character and end the suicide loop so I can start playing the game using my spare lives. And Ctrl + Shift + / just sends a command to press Home which is left-click in the game. The problem is that Ctrl + Shift + ] does not actually break the loop. It will rename my game character as I want by ControlSending F8 which is the keybind in the game to rename my character, but the automated nonstop suicide loop from Ctrl + Shift + [ never stops.

The script is below if someone can figure out what the problem is.

Code: Select all

#Requires AutoHotkey v2.0

target := 'ahk_exe svencoop.exe'

#HotIf WinExist(target)

^+[:: {

global BreakLoop
BreakLoop := False

Sleep(2000), ControlSend('{F7}')

Loop {

If (BreakLoop = True)
Break

Sleep( 500), ControlSend('{F10}')
Sleep( 500), ControlSend('{F10}')
Sleep(2500), ControlSend('{Home}')
Loop 2
Sleep(  25), ControlSend('{Space}')
Loop 15
Sleep(4000), ControlSend('{F10}')

}}

^+]:: {

global BreakLoop
BreakLoop := True

Sleep( 500), ControlSend('{F8}')

}

^+\:: {

Sleep(100), ControlSend('{Home}')

}

#HotIf

User avatar
boiler
Posts: 16954
Joined: 21 Dec 2014, 02:44

Re: My loop break isn't working. Trying to use one hotkey to end another hotkey's infinite loop.

Post by boiler » 22 May 2023, 22:36

Are you sure it never stops, or does it just not stop immediately? There’s nothing in the script that would have it break the loop before it finishes its current iteration, which is as much as several seconds.

gaveitatry
Posts: 13
Joined: 12 Feb 2023, 17:37

Re: My loop break isn't working. Trying to use one hotkey to end another hotkey's infinite loop.

Post by gaveitatry » 23 May 2023, 06:18

Oh, okay. I guess that would be the problem. Loops not breaking immediately, but breaking only after it reached the end of the loop.

So I went ahead and removed all the breakloop stuff and went back to using ::reload, which kills everything immediately. I set the ::reload to use a different hotkey. Shift + Esc.

+Esc::Reload

Thanks for the help everyone. The script would be working perfectly now.

Post Reply

Return to “Ask for Help (v2)”