hotkey command (release the last references to an object)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

hotkey command (release the last references to an object)

23 Apr 2017, 19:43

In the sample code below, and in order to release the last references to the object K, I bind the same key to another function (funcF) - otherwise, the __Delete meta-function isn't called. The use of Hotkey command here is intended for it that both !f and !e are in my script variables, read from a json file. Besides, I have a just about a dozen of context-sensitive hotkeys registered that way. Since my script could be subject to changes, I wonder if anybody had some idea or suggestion on how release the last caught references by other means, at least more reliable than unregistering the hard-code way all the concerned hotkeys. Many thanks.

EDIT:
Or - and since everything occurs here when the scripts exits - does exist a function that unregister indiscriminately all hotkeys, not needed anymore, in order here to free script objects? Thanks

Code: Select all

Class C {

	__New() {
	MsgBox, 64,, % A_ThisFunc
	}
	
	MFR(__param) {
	MsgBox % __param . " from " . A_ThisFunc
	}
	MES(__param) {
	MsgBox % __param . " from " . A_ThisFunc
	}
	
	__Delete() {
	MsgBox, 16,, % A_ThisFunc
	}


}
OnExit("ExitFunc")
K := new C()
var := K.MFR.bind(K, "Bonjour")
Hotkey, ifWinActive, ahk_class Notepad
Hotkey, !f, % var, on
var := K.MES.bind(K, "Hola")
Hotkey, ifWinActive, AutoHotkey Help ; ahk_exe hh.exe
Hotkey, !e, % var, on
return

!x::
ExitApp
ExitFunc(__exitReason, __exitCode) {
; Hotkey, ifWinActive, ahk_class Notepad
; Hotkey, !f, funcF, on
; Hotkey, ifWinActive, AutoHotkey Help ; ahk_exe hh.exe
; Hotkey, !e, funcF, on ; once these lines uncommented, __Delete meta-function is called
return 0
}

funcF() {
return true
}
my scripts
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: hotkey command (release the last references to an object)

28 Apr 2017, 20:11

I resolved to make a wrapper for the Hotkey command, keeping track of them, in order to resolve my problem, for lack of anything better.
Below an example that demonstrates how it works. I share it hoping it could be one way or another helpfull or suggest anything better to achieve it or give birth to
suggestions on the code itself.
Both Class Hotkeys and Hotkey are based on Run1e original work.

Code: Select all

#NoEnv
#Warn
#SingleInstance force

Class Hotkeys { ; a wrapper for some of the Hotkey command features based on Run1e original work (https://autohotkey.com/boards/viewtopic.php?f=6&t=28680)

	windows := [] ; keep track of hotkeys by their window scope since the same key can be bound to a function, label or boundfunc while you start to a different window scope.
	
	enable() { ; enableAll
		for __winTitle in this.windows
			for __key, __hotkey in this.windows[__winTitle]
				__hotkey.enable()
	}
	disable() { ; disableAll
		for __winTitle in this.windows
			for __key, __hotkey in this.windows[__winTitle]
				__hotkey.disable()
	}
	delete() { ; deleteAll
		for __winTitle in this.windows
			for __key, __hotkey in this.windows[__winTitle]
				__hotkey.delete()
	}
	
}
Class Hotkey {
	
	__New(__key, __target, __winTitle:=0, __winCondition:="Active") {
	
		if __winCondition not in Active,NotActive,Exist,NotExist
	return ErrorLevel:=4 ; 4 is also the concerned parameter index, usefull for debug
		
		if not (__label:=(IsLabel(__target) ? __target : this.handler.bind(this))) ; create a function reference (handler method) if it is a func or a boundfunc
	return ErrorLevel:=2 ; 2 is also the concerned parameter index, usefull for debug
	
		Hotkey, IfWin%__winCondition%, % __winTitle ? __winTitle : ""
		Hotkey, % __key, % __label, UseErrorLevel
		if (ErrorLevel)
	return ErrorLevel:=1
	
		this.key := __key, this.target := __target, this.winTitle := __winTitle, this.winCondition := __winCondition
		Hotkeys.windows[__winTitle] := IsObject(Hotkeys.windows[__winTitle]) ? Hotkeys.windows[__winTitle] : {}
		Hotkeys.windows[__winTitle][__key] := this
		
	return this
	}
	
	enable() {
	Hotkey, % "IfWin" . this.winCondition, % this.winTitle
	Hotkey, % this.key, On, UseErrorLevel
	}
	disable() {
	Hotkey, % "IfWin" . this.winCondition, % this.winTitle
	Hotkey, % this.key, Off, UseErrorLevel
	}
	delete() {
	static __f := Func("WinActive") ; just some random function to be set as label to release the last references that might be caught.
	Hotkey, % "IfWin" . this.winCondition, % this.winTitle
	Hotkey, % this.key, % __f, UseErrorLevel
	Hotkeys.windows[ this.winTitle ][ this.key ] := ""
	}
	
		handler() { ; not called, used for calling the target in case it is a func or boundfunc
			this.target.Call()
		}

}
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
Class C {


	__New() {
	MsgBox, 64,, % A_ThisFunc
	}
	
	MFR(__param) {
	MsgBox % __param . " from " . A_ThisFunc
	}
	MES(__param) {
	MsgBox % __param . " from " . A_ThisFunc
	}
	
	__Delete() {
	MsgBox, 16,, % A_ThisFunc
	}

}
OnExit("ExitFunc")
K := new C()
MyHotkey1 := new Hotkey("!i", K.MFR.bind(K, "Bonjour"), "AutoHotkey Help", "Active")
; MsgBox % ErrorLevel
MyHotkey2 := new Hotkey("!j", K.MES.bind(K, "Hola"), "ahk_class Notepad", "NotExist")
; MsgBox % ErrorLevel
return

!d::
MyHotkey1.disable()
MyHotkey2.disable()
return

!e::
MyHotkey1.enable()
MyHotkey2.enable()
return

!x::
ExitApp
ExitFunc(__exitReason, __exitCode) {
global
; MyHotkey1.delete()
; MyHotkey2.delete()
Hotkeys.delete() ; in order to release the last references and allows __Delete meta-function to be called
return 0
}
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Lamron750 and 240 guests