Cancelling hotkey actions

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Frozen Storm
Posts: 15
Joined: 24 Aug 2016, 09:43

Cancelling hotkey actions

22 Feb 2018, 16:00

I have the following code:

Code: Select all

origin := []

MoveMouse(xy) {
	MouseMove, xy[1], xy[2], 5.0
}

SetOrigin(pos:=false) {
	global origin
	if (pos)
		origin := pos
	else if (origin.Length() == 0) {
		MouseGetPos, x, y
		origin := [x, y]
	}
}

ReturnToOrigin() {
	global origin
	if (origin.Length() == 0)
		return
	MoveMouse(origin)
	SetOrigin([])
}

Wait(min, max) {
	Random, delay, min, max
	Sleep, delay
}

Space::
	ReturnToOrigin()
	Exit

F1::
	SetOrigin()
	
	MoveMouse([100, 100])
	Wait(1000, 3000)
	MoveMouse([500, 500])
	Wait(500, 1500)
	
	ReturnToOrigin()
	Exit

F2::
	SetOrigin()
	
	MoveMouse([1000, 1000])
	Wait(1000, 3000)
	MoveMouse([1500, 500])
	Wait(500, 1500)
	
	ReturnToOrigin()
	Exit
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: 15
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: 15
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

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: 301
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

origin := []
cancel := false ;(or true, doesn't really matter here)

MoveMouse(xy) {
	MouseMove, xy[1], xy[2], 5.0
}

SetOrigin(pos:=false) {
	global origin, cancel
	if (pos)
		origin := pos
	else if (origin.Length() == 0) {
		cancel := false
		MouseGetPos, x, y
		origin := [x, y]
	}
}

ReturnToOrigin() {
	global origin
	if (origin.Length() > 0) {
		MoveMouse(origin)
		SetOrigin([])
	}
	return
}

Wait(min, max) {
	Random, delay, min, max
	Sleep, delay
}

Space::
	cancel := True
	ReturnToOrigin()
	Return

F1::
	SetOrigin()
	if !(cancel) {
		MoveMouse([100, 100])
		Wait(1000, 3000)
		if !(cancel) {
			MoveMouse([500, 500])
			Wait(500, 1500)
			ReturnToOrigin()
		}
	}
	Return

F2::
	SetOrigin()
	if !(cancel) {
		MoveMouse([1000, 1000])
		Wait(1000, 3000)
		if !(cancel) {
			MoveMouse([1500, 500])
			Wait(500, 1500)
			ReturnToOrigin()
		}
	}
	Return
User avatar
jeeswg
Posts: 6902
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 ... 23#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

q:: ;a function to check whether to send a subroutine
global vDoExit := 0
MsgBox, 1
ExitCheck()
Sleep, 3000
ExitCheck()
Sleep, 3000
ExitCheck()
Sleep, 3000
ExitCheck()
MsgBox, 2
ExitCheck()

MsgBox, % "a"
ExitCheck()
MsgBox, % "b"
ExitCheck()
MsgBox, % "c"
ExitCheck()
MsgBox, % "d"
ExitCheck()
return

w:: ;end other subroutines
SoundBeep
vDoExit := 1
return

ExitCheck()
{
	if vDoExit
		Exit
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Noesis
Posts: 301
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 (v1)”

Who is online

Users browsing this forum: Descolada and 236 guests