Page 1 of 1

Loop toggle not working

Posted: 01 Sep 2019, 20:48
by eplefrek
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

Re: Loop toggle not working

Posted: 02 Sep 2019, 01:02
by Rohwedder
Hallo,
but you do know that the loop doesn't start until you press key 1?

Re: Loop toggle not working

Posted: 02 Sep 2019, 08:39
by eplefrek
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.

Re: Loop toggle not working

Posted: 02 Sep 2019, 10:02
by Rohwedder
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


Re: Loop toggle not working

Posted: 02 Sep 2019, 19:06
by eplefrek
It did stop when I pressed 2, but nothing happens when I press 2 (or 1) again.

Re: Loop toggle not working

Posted: 02 Sep 2019, 21:08
by Dumitas

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

Re: Loop toggle not working

Posted: 03 Sep 2019, 12:47
by eplefrek
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.

Re: Loop toggle not working

Posted: 03 Sep 2019, 17:07
by dobbelina

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

Re: Loop toggle not working

Posted: 03 Sep 2019, 17:22
by ShatterCoder
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

Re: Loop toggle not working

Posted: 04 Sep 2019, 07:32
by eplefrek
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

Re: Loop toggle not working

Posted: 04 Sep 2019, 07:53
by sinkfaze
Are you looking for the code to stop when the current iteration of the loop finishes, or break operation immediately?

Re: Loop toggle not working

Posted: 06 Sep 2019, 21:45
by eplefrek
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.

Re: Loop toggle not working

Posted: 09 Sep 2019, 08:24
by sinkfaze
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.

Re: Loop toggle not working

Posted: 04 Aug 2020, 09:54
by gh0st
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