How to make this timer stop execution the rest of my code

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
OCP
Posts: 98
Joined: 28 Mar 2018, 19:28

How to make this timer stop execution the rest of my code

24 May 2018, 14:46

hello i am stuck on this for a while

this code: once a button is clicked waits until next left click to execute on my mouse position i have been trying to include a timer in the code behind my button
if the timer is on zero i want it to escape the actions linked to that button
at the moment i have some escape options already: Esc, RButton, Insert, F12, Backspace
those work as they should

the timer does work only it does not stop the rest of the code as my Esc, RButton, Insert, etc. will do

i included a mini gui to show what is going on
it is very visual what is going on with splashtext if your interested to have a look
EQ2 is the one with the timer included on top

Code: Select all

 CoordMode, Mouse, Screen
#SingleInstance, Force

Gui, Add, Button, x2 y0 w163 h67 gEQ2, Test
Gui, Show, w195 h92, Untitled GUI
return

Timer()
{
	mouseGetPos,mx,my
	mx+=25
	my-=15
	Progress, cw000000 CTFFFFFF W150 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Canseled, , , Arial
	Sleep 400
	Progress,Off
	gosub, return
}

return:
return

GuiClose:
ExitApp

Timer:
Timer()
return 

EQ2: 
SetTimer, Timer, -2000
	;GuiControl,5:,Butto2,Assets/1/EQ2_on.png
	;While(GetKeyState("LButton")) 
	;Sleep,10
	;GuiControl,5:,Butto2,Assets/1/EQ2_off.png
	;IfWinExist, ahk_class TFXForm   
	;WinActivate
	while(!GetKeyState("LButton", "p"))
	{
		CoordMode, Mouse, Screen
		mouseGetPos,mx,my
		mx+=25
		my-=15
		Progress, cw000000 CTFFFFFF W170 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Insert EQ2, , , Arial
		{
			if(GetKeyState("Esc", "p"))
			{
				Send, {F9}
				mouseGetPos,mx,my
				mx+=25
				my-=15
				Progress, cw000000 CTFFFFFF W150 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Canseled, , , Arial
				Sleep 400
				Progress, Off
				Goto, return
			}
		else if(GetKeyState("RButton", "p"))
			{
				mouseGetPos,mx,my
				mx+=25
				my-=15
				Progress, cw000000 CTFFFFFF W150 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Canseled, , , Arial
				Sleep 400
				Progress, Off
				Goto, return
			}
		else if(GetKeyState("Insert", "p"))
			{
				mouseGetPos,mx,my
				mx+=25
				my-=15
				Progress, cw000000 CTFFFFFF W150 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Canseled, , , Arial
				Sleep 400
				Progress, Off
				Goto, return
			}
		else if(GetKeyState("F12", "p"))
            {
				mouseGetPos,mx,my
				mx+=25
				my-=15
				Progress, cw000000 CTFFFFFF W150 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Canseled, , , Arial
				Sleep 400
				Progress, Off
				Goto, return
			}
		else if(GetKeyState("Backspace", "p"))  
            {
				mouseGetPos,mx,my
				mx+=25
				my-=15
				Progress, cw000000 CTFFFFFF W150 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Canseled, , , Arial
				Sleep 400
				Progress, Off
				Goto, return
			}
		}
	}
	Progress, Off
	; rest of code here
	MsgBox, you clicked left to execute the rest
return
User avatar
Frosti
Posts: 426
Joined: 27 Oct 2017, 14:30
Contact:

Re: How to make this timer stop execution the rest of my code

24 May 2018, 17:17

You use a gosub from the inside your function to a label outside. I think this is not possible. You didn't receive any error message?
OCP
Posts: 98
Joined: 28 Mar 2018, 19:28

Re: How to make this timer stop execution the rest of my code

24 May 2018, 17:35

Frosti wrote:You use a gosub from the inside your function to a label outside. I think this is not possible. You didn't receive any error message?
at first i tried goto, return that give me an error so i tried gosub, return and that did not give a error this will just launch as is you can try it all works

besides that it only shows the canceled text and then is still waiting for my left click to execute the rest of the code

i am fairly new to ahk the solution lies probably in variables something i do not understand to implement at this time
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: How to make this timer stop execution the rest of my code

24 May 2018, 18:30

This will stop the timer and break the While loop....

Code: Select all

CoordMode, Mouse, Screen
#SingleInstance, Force

Gui, Add, Button, x2 y0 w163 h67 gEQ2, Test
Gui, Show, w195 h92, Untitled GUI
return

Timer()
{
	Global TimerOff := true
	mouseGetPos,mx,my
	mx+=25
	my-=15
	Progress, cw000000 CTFFFFFF W150 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Canseled, , , Arial
	Sleep 400
	Progress,Off
	SetTimer, Timer, Off
	gosub, return
}

return:
return

GuiClose:
ExitApp

Timer:
Timer()
return 

EQ2: 
SetTimer, Timer, -2000
	;GuiControl,5:,Butto2,Assets/1/EQ2_on.png
	;While(GetKeyState("LButton")) 
	;Sleep,10
	;GuiControl,5:,Butto2,Assets/1/EQ2_off.png
	;IfWinExist, ahk_class TFXForm   
	;WinActivate

	while(!GetKeyState("LButton", "p") && TimerOff != true )
	{
		CoordMode, Mouse, Screen
		mouseGetPos,mx,my
		mx+=25
		my-=15
		Progress, cw000000 CTFFFFFF W170 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Insert EQ2, , , Arial
		{
			if(GetKeyState("Esc", "p"))
			{
				Send, {F9}
				mouseGetPos,mx,my
				mx+=25
				my-=15
				Progress, cw000000 CTFFFFFF W150 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Canseled, , , Arial
				Sleep 400
				Progress, Off
				Goto, return
			}
		else if(GetKeyState("RButton", "p"))
			{
				mouseGetPos,mx,my
				mx+=25
				my-=15
				Progress, cw000000 CTFFFFFF W150 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Canseled, , , Arial
				Sleep 400
				Progress, Off
				Goto, return
			}
		else if(GetKeyState("Insert", "p"))
			{
				mouseGetPos,mx,my
				mx+=25
				my-=15
				Progress, cw000000 CTFFFFFF W150 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Canseled, , , Arial
				Sleep 400
				Progress, Off
				Goto, return
			}
		else if(GetKeyState("F12", "p"))
            {
				mouseGetPos,mx,my
				mx+=25
				my-=15
				Progress, cw000000 CTFFFFFF W150 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Canseled, , , Arial
				Sleep 400
				Progress, Off
				Goto, return
			}
		else if(GetKeyState("Backspace", "p"))  
            {
				mouseGetPos,mx,my
				mx+=25
				my-=15
				Progress, cw000000 CTFFFFFF W150 ZX1 ZY1 x%mx% y%my% m b fs25 zh0,Canseled, , , Arial
				Sleep 400
				Progress, Off
				Goto, return
			}
		}
	}
	Progress, Off
	; rest of code here
	MsgBox, you clicked left to execute the rest
return
...However in it's current state you're going end up running into spaghetti code, the script contains a lot of unnecessary repeats,
bad labeling and your hotkeys should probably be contextually bound.

Try something like:

Code: Select all

#SingleInstance, Force

Gui, Add, Button, x2 y0 w163 h67 gEQ2, Test
Gui, Show, w195 h92, Untitled GUI
return

EQ2:
SetTimer, Timer, -2000
ArmHotkeys := true
KillTimer( 170, mx, my, "Insert EQ2" )
return

#If ( ArmHotkeys = true ) 	; start contextual expression directive

; place your hotkeys in between the start and end #If directives

Esc::
Send, {F9}
KillTimer( 150, mx, my, "Canseled" )
; Goto, return 				; << not needed & bad naming convention!!
return

RButton::
KillTimer( 150, mx, my, "Canseled" )
return 

#If 						; end directive

Timer:
ArmHotkeys := false
KillTimer( 150, mx, my, "Canseled" )
return

KillTimer( width, mx, my, msg )
{
	CoordMode, Mouse, Screen
	mouseGetPos,mx,my
	Progress, cw000000 CTFFFFFF W%width% ZX1 ZY1 x%mx% y%my% m b fs25 zh0, % msg, , , Arial

	if ( msg = "Canseled" )
	{
		Sleep 400
		Progress, Off
		SetTimer, Timer, Off
	}
}
I omitted 3 of your hotkeys in the contextual area for you to figure out.

Please don't hesitate to ask if you can't get it to work.
User avatar
Frosti
Posts: 426
Joined: 27 Oct 2017, 14:30
Contact:

Re: How to make this timer stop execution the rest of my code

24 May 2018, 18:43

I ever thought it is impossible to use a gosub inside a function to jump to an outsided label. This is really working? It would open new ways to go for me.
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: How to make this timer stop execution the rest of my code

24 May 2018, 18:54

Yes you can jump out of a function

Code: Select all

SomeFunction()
Return

SomeLabel:
msgbox Label Fired!
Return

SomeFunction()
{
	gosub, SomeLabel
}
But the way your script is at present, it's a terrible idea going forward.
OCP
Posts: 98
Joined: 28 Mar 2018, 19:28

Re: How to make this timer stop execution the rest of my code

24 May 2018, 20:00

TLM wrote:This will stop the timer and break the While loop.......However in it's current state you're going end up running into spaghetti code, the script contains a lot of unnecessary repeats,
bad labeling and your hotkeys should probably be contextually bound.
thnx i have to look deeper into some of the things shown here, i plan to make 160 of such buttons...some streamlining might be in order,

i see some nice things here i have to play around with a bit more i might get there tomorrow i need some sleep first, i like to have the text following my mouse again, that was more important to me than the timer
KillTimer( 170, mx, my, "Insert EQ2" )

would you say i get problems with 160x such a piece of code in 1 script?

thnx for the help more stuff to study :)
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: How to make this timer stop execution the rest of my code

25 May 2018, 14:14

I forgot to remove the x y coordinate parameters

Code: Select all

KillTimer( width, msg ) ; remove mx & my parameters
{
	CoordMode, Mouse, Screen
	mouseGetPos,mx,my
	Progress, cw000000 CTFFFFFF W%width% ZX1 ZY1 x%mx% y%my% m b fs25 zh0, % msg, , , Arial

	if ( msg = "Canseled" )
	{
		Sleep 400
		Progress, Off
		SetTimer, Timer, Off
	}
}
OCP
Posts: 98
Joined: 28 Mar 2018, 19:28

Re: How to make this timer stop execution the rest of my code

25 May 2018, 16:33

TLM wrote:I forgot to remove the x y coordinate parameters
thnx for your help i have not fully figured it out jet (hoping for this oooooh moment i am studying your bits) i start from scratch tomorrow hope it all clicks at some point:)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Auntiejack56, JoeWinograd, Xtra and 141 guests