Start and Stop Buttons in GUI Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
leofabian
Posts: 12
Joined: 25 Jan 2022, 10:17

Start and Stop Buttons in GUI

Post by leofabian » 25 Jan 2022, 10:33

Hello to all!
I have written a script here with GUI that should start when the "Start" button is pressed and should be interrupted with the "Stop" button.
The script works the first time I press the "Start" button.
When I press "Stop" the script stops.
But when I press "Start" again, nothing happens.
What am I doing wrong?
I appreciate your answer!

Code: Select all

#SingleInstance, Force

Gui, Add, Button, x0 y21 w121 h29 gagain, Start
Gui, Add, Button, x130 y21 w121 h29 gstop, St&op
Gui, Add, Button, x260 y21 w121 h29 gover, E&xit

gui, show, w381 h50

stop:
Pause

again:
MouseMove, 20, 30, 50, R
sleep, 2000
MouseMove, -20, -30, 50, R
sleep, 2000
goto again

over:
ExitApp

GuiClose:
GuiEscape:
ExitApp

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Start and Stop Buttons in GUI

Post by boiler » 25 Jan 2022, 10:48

You are missing return statements in various places. Starting again won't undo a Pause. If you really want to use Pause to stop, then you should make it a start/stop toggle button. But to make it work using your arrangement, try this:

Code: Select all

#SingleInstance, Force

Gui, Add, Button, x0 y21 w121 h29 gagain, Start
Gui, Add, Button, x130 y21 w121 h29 gstop, St&op
Gui, Add, Button, x260 y21 w121 h29 gover, E&xit

gui, show, w381 h50
return

stop:
	stopped := 1
return

again:
	stopped := 0
	loop {
		MouseMove, 20, 30, 50, R
		sleep, 2000
		if stopped
			return
		MouseMove, -20, -30, 50, R
		sleep, 2000
		if stopped
			return
	}
return

over:
ExitApp

GuiClose:
GuiEscape:
ExitApp

leofabian
Posts: 12
Joined: 25 Jan 2022, 10:17

Re: Start and Stop Buttons in GUI

Post by leofabian » 25 Jan 2022, 10:54

Thank you, that does the job ;)

I admit that I am confused about "return".
I have seen many examples sometimes without and sometimes with.
I'll have to read up more on this.

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Start and Stop Buttons in GUI

Post by boiler » 25 Jan 2022, 11:08

By the way, if you are going to have separate start and stop buttons, it would make sense to disable the one that wouldn't make sense to choose:

Code: Select all

#SingleInstance, Force

Gui, Add, Button, x0 y21 w121 h29 gagain vStart, Start
Gui, Add, Button, x130 y21 w121 h29 gstop vStop Disabled, St&op
Gui, Add, Button, x260 y21 w121 h29 gover, E&xit

gui, show, w381 h50
return

stop:
	GuiControl, Enable, Start
	GuiControl, Disable, Stop
	stopped := 1
return

again:
	GuiControl, Enable, Stop
	GuiControl, Disable, Start
	stopped := 0
	loop {
		MouseMove, 20, 30, 50, R
		sleep, 2000
		if stopped
			return
		MouseMove, -20, -30, 50, R
		sleep, 2000
		if stopped
			return
	}
return

over:
GuiClose:
GuiEscape:
ExitApp

leofabian
Posts: 12
Joined: 25 Jan 2022, 10:17

Re: Start and Stop Buttons in GUI

Post by leofabian » 25 Jan 2022, 11:11

Boiler you're great.
Thank's a lot!

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Start and Stop Buttons in GUI  Topic is solved

Post by boiler » 25 Jan 2022, 11:42

No problem. Perhaps you'd like this better, where it uses one button for starting and stopping:

Code: Select all

#SingleInstance, Force

Gui, Add, Button, x0 y21 w121 h29 gstartstop vsstoggle, Start
Gui, Add, Button, x160 y21 w121 h29 gover, E&xit

gui, show, w281 h50

loop {
	while toggle {
		MouseMove, 20, 30, 50, R
		sleep, 2000
		if !toggle
			break
		MouseMove, -20, -30, 50, R
		sleep, 2000
	}
}
return

startstop:
	toggle := !toggle
	GuiControl,, sstoggle, % toggle ? "Stop" : "Start"
return

over:
GuiClose:
GuiEscape:
ExitApp

leofabian
Posts: 12
Joined: 25 Jan 2022, 10:17

Re: Start and Stop Buttons in GUI

Post by leofabian » 26 Jan 2022, 05:08

Yes, even better.
I love short code ;)
Thanks a lot!

Post Reply

Return to “Ask for Help (v1)”