Dynamic Hotkeys Topic is solved

Get help for the alpha version of AutoHotkey v2 here. Please state the v2 version you are working with in the title when making a new topic.
User avatar
TheArkive
Posts: 435
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Dynamic Hotkeys

23 Jun 2020, 12:28

I have a larger script that replicates the same behavior as this smaller example:

Code: Select all

var := "notepad++.exe"
hwnd := WinActive("ahk_exe " var)
HotIfWinActive "ahk_id " hwnd
Hotkey "^Space", "testCB"
HotIf

g := Gui.New("+AlwaysOnTop")
g.Add("Button","vBtn1","Test").OnEvent("click","gui_click")
g.Add("Button","vBtn2","Reset").OnEvent("click","gui_click")
g.Show("w200")

gui_click(ctl,info) {
	If (ctl.Name = "Btn1") {
		WinActivate "ahk_exe firefox.exe"
		HotIfWinActive "ahk_exe firefox.exe"
		Hotkey "^+Space", "testCB"
		HotIf
	} Else If (ctl.Name = "Btn2") {
		HotIfWinActive "ahk_exe firefox.exe"
		Hotkey "^+Space", "Off" ; shutting of hotkey is context sensitive?
		HotIf
	}
}


testCB(thisKey) {
	msgbox "hotkey: " thisKey
}
Basically, I can set the hotkey ^+Space with the Test button, and it only works in Firefox as expected, and I can clear/disable the hotkey with the Reset button. But when I try to set the hotkey again, it doesn't work. Just wondering what I'm missing.

Another thing that confused me is that the a112 help docs for HotIfWin... states:
... In other words, Hotkey A_ThisHotkey, "Off" turns off the current hotkey even if it is context-sensitive.
As far as I can tell that's what I'm trying to do with Hotkey "^+Space", "Off", but it doesn't seem to work unless I add HotIfWinActive ... in the line above.

Also, if I try to use HotIfWinExist, then it seems to set the hotkey as global, instead of being context sensitive with the specified window.

I'm just trying to wrap my brain around how this is meant to work. Basically I'm trying to set hotkeys dynamically so they can be user defined. In my larger script, it works on startup, but subsequent uses doesn't work at all.
swagfag
Posts: 4126
Joined: 11 Jan 2017, 17:59

Re: Dynamic Hotkeys

23 Jun 2020, 12:45

Hotkey "^+Space", "testCB" sets a hotkey's function to the supplied parameter(and creates the hotkey if it didnt exist already)
uve switched the hotkey Off, the second time u run this function all it does is change the hotkey's subroutine to what it already was. the hotkey remains Off. u need Hotkey "^+Space", "testCB", "On"
Hotkey A_ThisHotkey, "Off" has to be invoked by the hotkey's function for it to be able to modify the hotkey without having to specify the context
if I try to use HotIfWinExist, then it seems to set the hotkey as global, instead of being context sensitive with the specified window.
i doubt that
User avatar
TheArkive
Posts: 435
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: Dynamic Hotkeys

23 Jun 2020, 13:32

@swagfag
Thanks, I thought "Off" actually "unregistered" the hotkey. I had my wires crossed on that one. Now I get it. So, if a user changes hotkey settings in a GUI meant to modify how the script operates, the old hotkeys need to be set to "Off" and just ignored?
swagfag
Posts: 4126
Joined: 11 Jan 2017, 17:59

Re: Dynamic Hotkeys  Topic is solved

23 Jun 2020, 14:24

u can switch them off, u can govern their activation via a context sensitivity flag, u can substitute their subroutines with ones that dont do anything.
plenty of ways to go about it, its up to u
Last edited by swagfag on 23 Jun 2020, 16:41, edited 1 time in total.
User avatar
TheArkive
Posts: 435
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: Dynamic Hotkeys

23 Jun 2020, 16:02

Thanks man. That makes sense.

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: No registered users and 3 guests