Interrupt ONLY the current hotkey from a script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jorj escu
Posts: 6
Joined: 05 Jul 2019, 06:12

Interrupt ONLY the current hotkey from a script

29 Jul 2019, 05:54

Hello all,
It is possible to interrupt ONLY the current hotkey with an escape or other key, without reload, exitApp, which cause losing variables in the autoexecute area? Also, Pause command is not good because I don't want to resume that command (hotkey).
I have a script with a lot of hotkeys, and I want to interrupt at any point only the one which cause the problem, or if I run it by mistake, etc.
Rohwedder
Posts: 7719
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Interrupt ONLY the current hotkey from a script

29 Jul 2019, 10:40

Hallo,
use this sleep function and stop the current thread, while it sleeps.

Code: Select all

Global Exit
Return
Esc::Exit := True ;stops the current thread while it sleeps
q::
Loop, 25
{
	ToolTip,% "q-thread: " A_Index
	Sleep(200)
}
ToolTip
Return
w::
Loop, 20
{
	ToolTip,% "w-thread: " A_Index
	Sleep(250)
}
ToolTip
Return

Sleep(Time)
{ 
    End:= A_TickCount + Time
    While, (S:= End-A_TickCount) > 0
    {
        IF !Exit
			Sleep,S>100?100:S
		Else
		{
			Exit =
			Exit
		}			
	}
}
jorj escu
Posts: 6
Joined: 05 Jul 2019, 06:12

Re: Interrupt ONLY the current hotkey from a script

30 Jul 2019, 15:00

So the idea is to use the "Sleep(200)" function instead of native built-in "Sleep, 200" function from Autohotkey everywhere in my hotkeys?
Yes, it works and seems to be a good idea! Thank you!
jorj escu
Posts: 6
Joined: 05 Jul 2019, 06:12

Re: Interrupt ONLY the current hotkey from a script

30 Jul 2019, 23:37

The only issue is that when you press "Esc" key anywhere else, it will set the "Exit" variable, etc.
The workaround this seems to have the trigger key inside the function. Something like this, although I'm sure there are more elegant solutions:

Sleep(Time)
End:= A_TickCount + Time
While, (S:= End-A_TickCount) > 0
{
Tooltip, Sleeping %Time% ;%S%
KeyWait, Esc, D T0.01
If (ErrorLevel = 0)
{
tooltip, Stopped!
Exit
}
}
tooltip
}

Keywait does not accept expressions as parameters, and the the trigger key could be a function parameter, too.
Rohwedder
Posts: 7719
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Interrupt ONLY the current hotkey from a script

31 Jul 2019, 10:09

Like Windows 10, more elegant, but worse.
For example:

Code: Select all

Loop, 100
{
	Send, {q Down}
	Sleep, 200 ;native built-in Sleep
	Send, {q Up}
	Sleep(200) ;sleep function with Exit 
}
The first sleep must be native because a thread exit would leave the Q key hanging.
If you press the Escape button exactly during this first sleep, your elegant sleep function would not notice this.
But the Hotkey

Code: Select all

Esc::Exit := True
stored the exit request to be executed at the next sleep-function.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Leonardo_Portela, mikeyww, sachalamp and 205 guests