Loop toggle not working

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
eplefrek
Posts: 8
Joined: 27 Aug 2019, 12:45

Loop toggle not working

01 Sep 2019, 20:48

Im pretty new with ahk scripts and are trying to make a loop that you can toogle on and off. Everytime I press 2 to toggle the script, nothing happens.

Code: Select all

1::
Loop{
	Send,{LButton down}
	Send,{W down}
	Sleep,120000
	Send,{W up}
	Sleep,100
	Send,{S down}
	Sleep,120000
	Send,{S up}
	Send,{LButton up}
	Sleep,100
	Send,{t}
	Sleep,100
	Sendinput {raw}/fix
	Sleep,100
	Send,{enter}
	Sleep,100
}

2::pause, toggle  
Esc::ExitApp
Rohwedder
Posts: 7718
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Loop toggle not working

02 Sep 2019, 01:02

Hallo,
but you do know that the loop doesn't start until you press key 1?
eplefrek
Posts: 8
Joined: 27 Aug 2019, 12:45

Re: Loop toggle not working

02 Sep 2019, 08:39

Yes. I use it in minecraft when I mine. I click 1 and I walk forward and holding left button. Then when I press 2, I still walk forwards while pressing left button.
Rohwedder
Posts: 7718
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Loop toggle not working

02 Sep 2019, 10:02

Try:

Code: Select all

1::
Loop{
	Send,{LButton down}
	Send,{W down}
	Sleep,120000
	Send,{W up}
	Sleep,100
	Send,{S down}
	Sleep,120000
	Send,{S up}
	Send,{LButton up}
	Sleep,100
	Send,{t}
	Sleep,100
	Sendinput {raw}/fix
	Sleep,100
	Send,{enter}
	Sleep,100
	}
Return

2::
Send,{LButton Up}{w Up}{s Up} ;prevents from staying down
pause, toggle, 1 ;OperateOnUnderlyingThread
Return
Esc::ExitApp

eplefrek
Posts: 8
Joined: 27 Aug 2019, 12:45

Re: Loop toggle not working

02 Sep 2019, 19:06

It did stop when I pressed 2, but nothing happens when I press 2 (or 1) again.
User avatar
Dumitas
Posts: 167
Joined: 14 Dec 2017, 21:32

Re: Loop toggle not working

02 Sep 2019, 21:08

Code: Select all

#MaxThreadsPerHotkey 2
1::
toggle:=!toggle
while, toggle
{
	Send,{LButton down}
	Send,{W down}
	Sleep,120000
	Send,{W up}
	Sleep,100
	Send,{S down}
	Sleep,120000
	Send,{S up}
	Send,{LButton up}
	Sleep,100
	Send,{t}
	Sleep,100
	Sendinput {raw}/fix ; What's that?
	Sleep,100
	Send,{enter}
	Sleep,100
}
Return
Image
eplefrek
Posts: 8
Joined: 27 Aug 2019, 12:45

Re: Loop toggle not working

03 Sep 2019, 12:47

The code works, but it does not toggle, unless im supposed to press something else than "1" on this script, since you removed the "2".

But the
Send,{t}
Sleep,100
Sendinput {raw}/fix ; What's that?
Sleep,100
Send,{enter}
Sleep,100
is for the script to type /fix in chat so my pickaxe/tool dont get destroyed while I afk with this script. Pressing "t" opens the chat in minecraft.
dobbelina
Posts: 39
Joined: 03 Sep 2019, 02:45
Contact:

Re: Loop toggle not working

03 Sep 2019, 17:07

Code: Select all

#MaxThreadsPerHotkey 2
1::
toggle:=!toggle
Loop
{
    If not Toggle
        break
	Send,{LButton down}
	Send,{W down}
	Sleep,120000
	Send,{W up}
	Sleep,100
	Send,{S down}
	Sleep,120000
	Send,{S up}
	Send,{LButton up}
	Sleep,100
	Send,{t}
	Sleep,100
	Sendinput {raw}/fix ; What's that?
	Sleep,100
	Send,{enter}
	Sleep,100
}
Return
ShatterCoder
Posts: 84
Joined: 06 Oct 2016, 15:57

Re: Loop toggle not working

03 Sep 2019, 17:22

These types of scripts are best done by making the keyboard hook explicit with the '$' sign (credit to tidbit https://www.autohotkey.com/boards/viewtopic.php?t=11952). Also it may or may not help to explicitly set toggle to a specific state prior to calling the hotkey.

Code: Select all

#MaxThreadsPerHotkey 2
toggle := 0
return

$1::
toggle:=!toggle
Loop
{
    If not Toggle
        break
	Send,{LButton down}
	Send,{W down}
	Sleep,120000
	Send,{W up}
	Sleep,100
	Send,{S down}
	Sleep,120000
	Send,{S up}
	Send,{LButton up}
	Sleep,100
	Send,{t}
	Sleep,100
	Sendinput {raw}/fix ; What's that?
	Sleep,100
	Send,{enter}
	Sleep,100
}
Return
eplefrek
Posts: 8
Joined: 27 Aug 2019, 12:45

Re: Loop toggle not working

04 Sep 2019, 07:32

Either im doing something wrong, or im asking for something for thats not possible. Since once again, both scripts work, but don't seem to toggle. maybe its not meant to be
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Loop toggle not working

04 Sep 2019, 07:53

Are you looking for the code to stop when the current iteration of the loop finishes, or break operation immediately?
eplefrek
Posts: 8
Joined: 27 Aug 2019, 12:45

Re: Loop toggle not working

06 Sep 2019, 21:45

Hey! Sorry for late response!

If I understand you correctly, I want it to break operation immediately. I will try and show you what I mean with these videos.

The script I made/trying to make is doing similar to what this person is doing: https www.youtube.com /watch?v=eOKBCIohycU&t=3358s Broken Link for safety (he is turning around at the end instead of just walking backward)

The problem is, they sometimes send you a captcha to check if you are there or not (like this: https youtu.be /iqIJ7bdhFf4?t=8755). Broken Link for safety So i need to be able to toggle it on and off when the captcha shows up.
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Loop toggle not working

09 Sep 2019, 08:24

I can't watch the video at the moment, but if you need to break operation immediately then 2::Reload is probably your best bet. But time is going to be going by fast in your script, so at least some keystrokes beyond what you need are going to get buffered and executed despite your reload. It will probably be better to put other measures in place to break the script, like an ImageSearch that will look for the Captcha image on the page and induce a break.
gh0st
Posts: 1
Joined: 04 Aug 2020, 09:49

Re: Loop toggle not working

04 Aug 2020, 09:54

Created my account to thank you for this. This corrected my issue about my script not toggling off.

Here is my script for those interested.

Code: Select all

$^e::
Toggle := !Toggle
Loop
{
	If !Toggle
		break
	Send,{e}
	Sleep,10
}
return
ShatterCoder wrote:
03 Sep 2019, 17:22
These types of scripts are best done by making the keyboard hook explicit with the '$' sign (credit to tidbit https://www.autohotkey.com/boards/viewtopic.php?t=11952). Also it may or may not help to explicitly set toggle to a specific state prior to calling the hotkey.

Code: Select all

#MaxThreadsPerHotkey 2
toggle := 0
return

$1::
toggle:=!toggle
Loop
{
    If not Toggle
        break
	Send,{LButton down}
	Send,{W down}
	Sleep,120000
	Send,{W up}
	Sleep,100
	Send,{S down}
	Sleep,120000
	Send,{S up}
	Send,{LButton up}
	Sleep,100
	Send,{t}
	Sleep,100
	Sendinput {raw}/fix ; What's that?
	Sleep,100
	Send,{enter}
	Sleep,100
}
Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CoffeeChaton, Rohwedder and 151 guests