1

Whenever you press w you get into a loop that press e around every 10 seconds. Another button has to be pressed to get out of it again at any moment (and making it possible to start over again). This is what I have so far:

w::
Loop
{
Send, e
Random, SleepAmount, 9000, 10000
Sleep, %SleepAmount%
x::Break
}
Return  

I don't understand why it is not working yet. It presses e once and doesn't do anything anymore after that.

user3604398
  • 31
  • 1
  • 1
  • 3

1 Answers1

2
x::Break

is the short form for

x::
    break
return

and therefore terminates the current subroutine. Never define a hotkey within any other execution bodies. Instead, define the x-hotkey outside the w hotkey and make it stop the loop.

Example using goTo (note that goSub is different for the latter will not terminate the subroutine):

w::
    Loop {
        send e
        Random, SleepAmount, 9000, 10000
        Sleep, %SleepAmount%
    }
    after_loop:
return

x::
    goTo after_loop
return
; or, more compact:
; x::goto after_loop

Since gotos are pretty bad programming style, you might consider using a timer (instead of the loop). But to be honest, it's not worth it because your sleep amount is not fix.

phil294
  • 10,038
  • 8
  • 65
  • 98
  • I can't test this right now, but this old answer of mine seems to be nonsense: the `x` hotkey spawns a new thread that just goes to `after_loop` and then stops again. It does not change the *other* thread of `w::` which will keep running regardless of the other one... Instead, a variable should be set in `x` and `w` should break based on that – phil294 Mar 14 '23 at 23:05