Help with Code, simple butfustrating Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mexican scientist
Posts: 33
Joined: 02 Jul 2020, 21:55

Help with Code, simple butfustrating

05 Jan 2023, 18:45

I have this code that loops and generates a random number between 1 and 100000 and if the number is less than 95000 then it holds 1 down for 200 ms. Else, it holds 1 down for 2000 ms. If I press 9, then the loop stops, but I want my code to do more when I press 9. If I press 9, I want the code to stop the loop AND hold down 1 for 2000 ms. How would I do such thing?

Code: Select all

$2::
stop := 0
Loop
{
	Random, Rand, 1, 100000
	IF (Rand <= 95000) ;with 95% chance
		{
		sendinput, {1 down}
		sleep, 200
		sendinput, {1 up}
		}
	Else 
		{
		sendinput, {1 down}
		sleep, 2000
		sendinput, {1 up}
		}
}until Stop


9::Stop := 1
return
I tried this

Code: Select all

9::Stop := 1
sendinput, {1 down}
sleep, 2000
sendinput, {1 up}
but the loops stops and the rest of the code is not executed.
User avatar
andymbody
Posts: 939
Joined: 02 Jul 2017, 23:47

Re: Help with Code, simple butfustrating

05 Jan 2023, 19:07

Try this... drop the Stop := 1 down one line. Also include returns for each key section. I personally use While(!Stop) for this, rather than Loop. To me it's much more clear.

Code: Select all

#SingleInstance, force

return
$2::
stop := 0
Loop
{
	Random, Rand, 1, 100000
	IF (Rand <= 95000) ;with 95% chance
	{
		sendinput, {1 down}
		sleep, 200
		sendinput, {1 up}
	}
	Else 
	{
		sendinput, {1 down}
		sleep, 2000
		sendinput, {1 up}
	}
} until Stop
return

9::
Stop := 1	; drop down one line
sendinput, {1 down}
sleep, 2000
sendinput, {1 up}
return

mexican scientist
Posts: 33
Joined: 02 Jul 2020, 21:55

Re: Help with Code, simple butfustrating

05 Jan 2023, 19:14

andymbody wrote:
05 Jan 2023, 19:07
Try this... drop the Stop := 1 down one line. Also include returns for each key section. I personally use While(!Stop) for this, rather than Loop. To me it's much more clear.

Code: Select all

#SingleInstance, force

return
$2::
stop := 0
Loop
{
	ToolTip, % "in loop"
	Random, Rand, 1, 100000
	IF (Rand <= 95000) ;with 95% chance
	{
		sendinput, {1 down}
		sleep, 200
		sendinput, {1 up}
	}
	Else 
	{
		sendinput, {1 down}
		sleep, 2000
		sendinput, {1 up}
	}
} until Stop
return

9::
Stop := 1	; drop down one line
sendinput, {1 down}
sleep, 2000
sendinput, {1 up}
return


Thank you, this works. I removed the code

Code: Select all

ToolTip, % "in loop"
because it i want this for a game. It looks like the only thing missing was the code

Code: Select all

#SingleInstance, force
Thank you!
User avatar
andymbody
Posts: 939
Joined: 02 Jul 2017, 23:47

Re: Help with Code, simple butfustrating

05 Jan 2023, 19:31

I removed the code... Tooltip
lol... I forgot to remove that before posting... I was using it for test the loop
mexican scientist
Posts: 33
Joined: 02 Jul 2020, 21:55

Re: Help with Code, simple butfustrating

05 Jan 2023, 20:14

andymbody wrote:
05 Jan 2023, 19:31
I removed the code... Tooltip
lol... I forgot to remove that before posting... I was using it for test the loop
Do you mind If i ask for help with a similar problem but now with a different code? The code does the same thing, but this time I control the loop with the same activation key which is #2

Code: Select all

2::
SetTimer VAR,% (VAR:=!VAR)?200:"Off"
IF !RBraket
	return
VAR:
	Random, Rand, 1, 100000
	IF (Rand <= 95000) ;with 95% chance
		{
		sendinput, {1 down}
		sleep, 200
		sendinput, {1 up}
		}
	Else 
		{
		sendinput, {1 down}
		sleep, 2000
		sendinput, {1 up}
		}
Return
Basically, if I press #2, the code executes. If I press #2 again the code halts, if i press #2 again the code starts again etc.... So basically #2 acts like an ON and OFF switch.

When I press 9, I want the code to halt or resume, just as if I was pressing the #2 key with my finger. This is the code I have:

Code: Select all

9::
sendinput, {2 down}
sleep, 100
sendinput, {2 up}
return
But it does not work! The code never stops, its as if nothing is pressing #2.


[Mod edit: Changed a quote set of quote tags to code tags.]
User avatar
andymbody
Posts: 939
Joined: 02 Jul 2017, 23:47

Re: Help with Code, simple butfustrating

05 Jan 2023, 20:47

SetTimer VAR,% (VAR:=!VAR)?200:"Off"
I don't recommend using same name for the label and the toggle variable


Also... separate the label and the hotkeys....

try this... run it as is, to see that it is toggling with both 2 and 9 then you can remove the tooltips and comments from your code to test the rest

Code: Select all

#SingleInstance, force

c := 0
toggle := false
return

2::
SetTimer rndm, % (toggle := !toggle) ? 200 : "Off"
return

9::
sendinput, 2
return


rndm:
	Random, Rand, 1, 100000
	c++
	IF (Rand <= 95000) ;with 95% chance
		{
		ToolTip, % "c := " . c . "`nsleep := 200"
;		sendinput, {1 down}
;		sleep, 200
;		sendinput, {1 up}
		}
	Else 
		{
		ToolTip, % "c := " . c . "`nsleep := 2000"
;		sendinput, {1 down}
;		sleep, 2000
;		sendinput, {1 up}
		}
Return

esc::ExitApp
mexican scientist
Posts: 33
Joined: 02 Jul 2020, 21:55

Re: Help with Code, simple butfustrating

05 Jan 2023, 22:00

andymbody wrote:
05 Jan 2023, 20:47
SetTimer VAR,% (VAR:=!VAR)?200:"Off"
I don't recommend using same name for the label and the toggle variable


Also... separate the label and the hotkeys....

try this... run it as is, to see that it is toggling with both 2 and 9 then you can remove the tooltips and comments from your code to test the rest

Code: Select all

#SingleInstance, force

c := 0
toggle := false
return

2::
SetTimer rndm, % (toggle := !toggle) ? 200 : "Off"
return

9::
sendinput, 2
return


rndm:
	Random, Rand, 1, 100000
	c++
	IF (Rand <= 95000) ;with 95% chance
		{
		ToolTip, % "c := " . c . "`nsleep := 200"
;		sendinput, {1 down}
;		sleep, 200
;		sendinput, {1 up}
		}
	Else 
		{
		ToolTip, % "c := " . c . "`nsleep := 2000"
;		sendinput, {1 down}
;		sleep, 2000
;		sendinput, {1 up}
		}
Return

esc::ExitApp
Thank you, but the code is a little bit conflicting... it is my fault for not expressing in detail what I wanted to achieve. Basically, #2 is the ON and Off switch, but #9 must be to OFF switch at all times, so that if I press #9 it should not activate #2. If 2:: is running, then 9 sets it OFF.

Code: Select all

9::
; I have the idea in my mind, but I don't know how to implement it, the logical steps go like this:
if (2:: is running, then switch it OFF)
	sleep, 100
	sendinput, {1 down}
	sleep, 2000
	sendinput, {1 up}
Else
	sleep, 100
	sendinput, {1 down}
	sleep, 2000
	sendinput, {1 up}
User avatar
andymbody
Posts: 939
Joined: 02 Jul 2017, 23:47

Re: Help with Code, simple butfustrating  Topic is solved

05 Jan 2023, 23:41

When I press 9, I want the code to halt or resume, just as if I was pressing the #2 key with my finger.
Your previous description is what my previous post provides...

Code: Select all

9::
sendinput, 2
return

But, your latest description is different...So I'm a bit confused...
Basically, #2 is the ON and Off switch, but #9 must be to OFF switch at all times, so that if I press #9 it should not activate #2. If 2:: is running, then 9 sets it OFF.
Below should now accomplish your latest description

Code: Select all

9::
; if you do not want the timer to be active when 9 is pressed...
; ...no need to check whether 2 is on/off... 
; ...just always set to off regardless
toggle := false
SetTimer, rndm, off

; these will always execute once when pressing 9, no matter what
sendinput, {1 down}
sleep, 2000
sendinput, {1 up}
return
If this is still not correct... please provide pseudo-code (actual description using normal english words - no AHK code) to explain exactly what you want to see happen, step by step.

Andy

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: cgx5871 and 167 guests