Cancelling hotkey actions

Get help with using AutoHotkey and its commands and hotkeys
Frozen Storm
Posts: 8
Joined: 24 Aug 2016, 09:43

Cancelling hotkey actions

22 Feb 2018, 16:00

I have the following code:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


The basic idea is to do some stuff when I press F1 or F2. However, when I press Space while one of the two is executing, I'd like it to cancel any further actions and return the mouse to its original position (its position before I clicked F1 or F2). Additionally, if I press F2 while F1's body is executing, I'd like it to immediately start executing F2's body and when it's done it should not return to F1's body.

I initially had Returns instead of Exits and thought the Exits would fix it they didn't. If I press Space, the mouse returns to origin, but then F1/F2 continues executing, and the same issue happens when I press F1 first and then F2. How do I properly cancel hotkey actions?
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Cancelling hotkey actions

23 Feb 2018, 02:07

I don't understand why you put all those Exits everywhere. Anyways I had a similar situation what I did was put a line for stopping the routine if cancelled after ever single line but I'm sure there is a better way.
I am your average ahk newbie. Just.. a tat more cute. ;)
Frozen Storm
Posts: 8
Joined: 24 Aug 2016, 09:43

Re: Cancelling hotkey actions

23 Feb 2018, 07:05

Well I put the Exits there because I was hoping that would fully Exit the execution of all hotkeys (which it doesn't, unfortunately). I also thought #MaxThreads 1 could possibly help but the docs say that #MaxThreads should not be set to 1.
Frozen Storm
Posts: 8
Joined: 24 Aug 2016, 09:43

Re: Cancelling hotkey actions

23 Feb 2018, 14:28

I've managed to find a temporary solution, Reloading the script every time after it returns to origin.

Code: [Select all] [Download] GeSHi © Codebox Plus

ReturnToOrigin() {
global origin
if (origin.Length() == 0)
return
MoveMouse(origin)
Reload
}


This works, but in the actual script this makes it constantly have to reinitialize everything (which includes reading from files, etc.). Is there a better solution to this?
Noesis
Posts: 284
Joined: 26 Apr 2014, 07:57

Re: Cancelling hotkey actions

24 Feb 2018, 00:50

Just use a variable and check it, before executing each action:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 4508
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Cancelling hotkey actions

26 Feb 2018, 04:40

- This approach may be useful, creating a temporary second script.
Stop a function by calling it again? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=40506&p=185023#p185023

- Can this script be improved? There's a delayed reaction during the Sleep period when you press the secondary hotkey to end the script.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Noesis
Posts: 284
Joined: 26 Apr 2014, 07:57

Re: Cancelling hotkey actions

26 Feb 2018, 23:23

jeeswg wrote:- Can this script be improved? There's a delayed reaction during the Sleep period when you press the secondary hotkey to end the script.


It could, in that the delayed reaction could be reduced to anything down to about 15ms so made to look negligible, (fyi the code I submitted has the same delayed reaction issue as yours) but the real question is whether it's actually worth improving, and that answer totally depends on the planned usage.

In order to do it one would have to change the methodology used in implementing it, instead of using traditional functions/subroutines with sleeps, you'd use a timer routine/function instead avoiding the usage of sleeps (the sleeps are what cause the delay).

The timer routine would need data corresponding to an action's next action time (it's next run-time so to speak). The timer would run at a constant (small) frequency, checking if the action is cancelled and if not also checking the Current Tickcount against the next action time data, to see if the action is to perform, or if the timer should just finish thus wait for the next cycle. Not hard to do but increases the complexity, and most the time it's probably not worth it.

Return to “Ask For Help”

Who is online

Users browsing this forum: Mingo, tyyi and 15 guests