Unable to interrupt/exit/pause script.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MichaelAHK
Posts: 5
Joined: 15 Jun 2022, 09:24

Unable to interrupt/exit/pause script.

Post by MichaelAHK » 15 Jun 2022, 09:32

Hello,

I am having issues getting my script to stop when I press a hotkey. I tried essentially all variations of ExitApp, Suspend, and Pause in basically every location in my code. most of the time the hotkey simply does nothing. I suspect it is because my loops take a long time to complete, and It can't stop the code in the middle of a loop. Just a guess though. Here is my code:

Code: Select all

F4::
F7::ExitApp
Loop{

Loop, 40
{
Random, rand1, 2000, 3000
Random, rand2, 44000, 45000
Random, rand3, 500, 750 
Random, rand4, 45000, 45000
Send {Click, Down}
Send {d down}
Sleep, rand2
Send {d up}
Sleep, rand1
Send {a down}
Sleep, rand4
Send {a up}
SoundBeep, 1500
}
DllCall("mouse_event", uint, 1, int, 1200, int, 0)
Send {w down}
Sleep, 1000
Send {w up}

Loop, 40
{
Random, rand1, 500, 750 
Random, rand2, 44000, 45000
Random, rand3, 500, 750 
Random, rand4, 44000, 45000
Send {Click, Down}
Send {a down}
Sleep, rand2
Send {a up}
Sleep, rand1
Send {d down}
Sleep, rand4
Send {d up}
SoundBeep, 1500
}
DllCall("mouse_event", uint, 1, int, 1200, int, 0)
Send {w down}
Sleep, 1000
Send {w up}

}

Return

Rohwedder
Posts: 7645
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Unable to interrupt/exit/pause script.

Post by Rohwedder » 15 Jun 2022, 09:48

Hallo,
unable to interrupt/exit/pause script?
Strange! Your script effectively consists only of:

Code: Select all

F4::
F7::ExitApp
everything else is unreachable code:

Code: Select all

F4::
F7::ExitApp
Loop
{
	Loop, 40
	{
		Random, rand1, 2000, 3000
		Random, rand2, 44000, 45000
		Random, rand3, 500, 750
		Random, rand4, 45000, 45000
		Send {Click, Down}
		Send {d down}
		Sleep, rand2
		Send {d up}
		Sleep, rand1
		Send {a down}
		Sleep, rand4
		Send {a up}
		SoundBeep, 1500
	}
	DllCall("mouse_event", uint, 1, int, 1200, int, 0)
	Send {w down}
	Sleep, 1000
	Send {w up}
	Loop, 40
	{
		Random, rand1, 500, 750
		Random, rand2, 44000, 45000
		Random, rand3, 500, 750
		Random, rand4, 44000, 45000
		Send {Click, Down}
		Send {a down}
		Sleep, rand2
		Send {a up}
		Sleep, rand1
		Send {d down}
		Sleep, rand4
		Send {d up}
		SoundBeep, 1500
	}
	DllCall("mouse_event", uint, 1, int, 1200, int, 0)
	Send {w down}
	Sleep, 1000
	Send {w up}
}
Return
By the way, shouldn't the DllCall be like this?:

Code: Select all

DllCall("mouse_event", "UInt", 1, "UInt", 1200, "UInt", 0)

MichaelAHK
Posts: 5
Joined: 15 Jun 2022, 09:24

Re: Unable to interrupt/exit/pause script.

Post by MichaelAHK » 15 Jun 2022, 11:38

Rohwedder wrote:
15 Jun 2022, 09:48
Hallo,
unable to interrupt/exit/pause script?
Strange! Your script effectively consists only of:

Code: Select all

F4::
F7::ExitApp
everything else is unreachable code:

Code: Select all

F4::
F7::ExitApp
Loop
{
	Loop, 40
	{
		Random, rand1, 2000, 3000
		Random, rand2, 44000, 45000
		Random, rand3, 500, 750
		Random, rand4, 45000, 45000
		Send {Click, Down}
		Send {d down}
		Sleep, rand2
		Send {d up}
		Sleep, rand1
		Send {a down}
		Sleep, rand4
		Send {a up}
		SoundBeep, 1500
	}
	DllCall("mouse_event", uint, 1, int, 1200, int, 0)
	Send {w down}
	Sleep, 1000
	Send {w up}
	Loop, 40
	{
		Random, rand1, 500, 750
		Random, rand2, 44000, 45000
		Random, rand3, 500, 750
		Random, rand4, 44000, 45000
		Send {Click, Down}
		Send {a down}
		Sleep, rand2
		Send {a up}
		Sleep, rand1
		Send {d down}
		Sleep, rand4
		Send {d up}
		SoundBeep, 1500
	}
	DllCall("mouse_event", uint, 1, int, 1200, int, 0)
	Send {w down}
	Sleep, 1000
	Send {w up}
}
Return
By the way, shouldn't the DllCall be like this?:

Code: Select all

DllCall("mouse_event", "UInt", 1, "UInt", 1200, "UInt", 0)
Hello,

Thanks for the reply. For whatever reason, the code you pasted (with the indentation) does not work, it only works when you get rid of the F7::ExitApp line. my previous code does work (for some reason) however the the ExitApp hotkey doesn't work. as for the DllCall, it works as intended, though again, I'm not sure why.

User avatar
kunkel321
Posts: 1058
Joined: 30 Nov 2015, 21:19

Re: Unable to interrupt/exit/pause script.

Post by kunkel321 » 15 Jun 2022, 13:53

I'm not an expert, but I think
F4::
causes it to play what is beneath it.

However when the code is all on one line like:
F7::ExitApp
Then that line of code is an "entity" of it's own. It waits for F7, then plays ExitApp.

So the top of your script is: "Wait for F4 keypress and play everything until you get to another hotkey, then stop." So it stops when it sees F7::.

Try putting

Code: Select all

F4::
;your code here
Return

F7::ExitApp ;at the bottom.
EDIT: As a side note: The indentation is just to make the code "human-friendly." The AutoHotkey engine doesn't care, so it shouldn't change how the code runs.
ste(phen|ve) kunkel

MichaelAHK
Posts: 5
Joined: 15 Jun 2022, 09:24

Re: Unable to interrupt/exit/pause script.

Post by MichaelAHK » 30 Jun 2022, 07:43

If I put the exitapp hotkey after the return function, do I need to wait for the script to complete before being able to activate it? also with it immediately interrupt the script? Thanks.

User avatar
kunkel321
Posts: 1058
Joined: 30 Nov 2015, 21:19

Re: Unable to interrupt/exit/pause script.

Post by kunkel321 » 30 Jun 2022, 08:39

MichaelAHK wrote:
30 Jun 2022, 07:43
If I put the exitapp hotkey after the return function, do I need to wait for the script to complete before being able to activate it? also with it immediately interrupt the script? Thanks.
I believe the ahk script will exit completely (and immediately) from RAM if you press F7. Even if you press it mid-loop. I'm not 100% sure though. I don't know anything about DLLCalls, so I can't comment on whether that would be immediately stopped.
ste(phen|ve) kunkel

Post Reply

Return to “Ask for Help (v1)”