How do I switch between sets of hotkeys using Hotkey, If according to a variable's contents? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Off Topic
Posts: 43
Joined: 07 Oct 2017, 20:57

How do I switch between sets of hotkeys using Hotkey, If according to a variable's contents?

22 Apr 2018, 10:45

Hi guys, I have different sets of Hotkeys that I'd like to use for Atom. Problem is that I have three viewport panels inside Atom (for separate HTML, CSS, and JS) at any given time, and I know that I can easily use InStr() to determine which one of those I'm working with, but I'm having a lot of trouble assigning and toggling different Hotkey commands unless I resort to the entire IfWinActive, ahk_exe atom.exe for any particular one. I was hoping I could have it auto-executed like so, and the hotkeys would be conditional to the contents of a SubTitle variable:

Code: Select all

	global	HTMLKeys :=  		{"^XButton1" : "^]"
								,"^XButton2" : "^["
								,"^Numpad7" : "<link href=""./resources/css/style.css"" type=""text/css"" rel=""stylesheet"">"
								,"<!Left" : "{Home}"
								,"<!Right" : "{End}"
								,"^RButton" : "^/"}
	; Hotkey, IfWinActive, ahk_exe atom.exe
	Hotkey, If, SubTitle := "HTML"
	for index, value in HTMLKeys {
	    bf  :=  Func("KeySet").bind(value)
	    Hotkey, % index, % bf, On
	}
	Hotkey, If


	global 	CSSKeys :=  		{"^NumpadDiv" : "border: 2px solid black;{Enter}"
								,"^Numpad8" : "resources\images\"
							    ,"^Numpad9" : "url(""../images/"");{Left 3}"}
	; Hotkey, IfWinActive, ahk_exe atom.exe
	Hotkey, If, SubTitle := "CSS"
	for index, value in CSSKeys {
	    bf  :=  Func("KeySet").bind(value)
	    Hotkey, % index, % bf, On
	}
	Hotkey, If

	global 	JavaScriptKeys :=  	{"^l" : "console.log(){Left}"
								, "^Numpad7" : """"" : """""
							    ,"^Enter" : "(function() {{Enter}});"}
    ; Hotkey, IfWinActive, ahk_exe atom.exe
	Hotkey, If, SubTitle := "JavaScript"
	for index, value in JavaScriptKeys {
	    bf  :=  Func("KeySet").bind(value)
	    Hotkey, % index, % bf, On
	}
	Hotkey, If


	KeySet(msg) {
	    SendInput, % msg
	}


	#If SubTitle := "HTML"
	Enter::Send, {Enter}
	#If

	#If SubTitle := "CSS"
	Enter::Send, {Enter}
	#If

	#If SubTitle := "JavaScript"
	Enter::Send, {Enter}
	#If
Using these #If statements doesn't yield errors. If this were possible, it'd make it very easy for me because I could just change the SubTitle variable and run a function that toggles the corresponding keys on. The documentation for Hotkey Command implies that you can do this (Hotkey, If, (Expression)), but it has no examples of it and I feel the two shown are either extremely simple or pretty complex with nothing between. I'm thinking that I would need to assign them, disable all of them, then toggle one at a time to reactivate the hotkeys:

Code: Select all

MButton::
	; This would be automatic according to WinTitle
	++Toggle
	If (Toggle = 1) {
		SubTitle := "HTML"
		AtomKeys("HTML")
	} If (Toggle = 2) {
		SubTitle := "CSS"
		AtomKeys("CSS")
	} If (Toggle = 3) {
		SubTitle := "JavaScript"
		AtomKeys("JavaScript")
		Toggle = 0
	}
	Return

	AtomKeys(Var) {
	for index, value in %Var%Keys {
		    Hotkey, % index, Toggle
		}
	}
	Return
But I'm not sure if it's really possible to do it this way to begin with or if I'm heading in the wrong direction. Is this possible? Can I get any help?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How do I switch between sets of hotkeys using Hotkey, If according to a variable's contents?  Topic is solved

22 Apr 2018, 13:20

Code: Select all

; these need to exist somewhere in the script
; for custom "Hotkey, If"s to work
#If, (SubTitle == "HTML")
#If, (SubTitle == "CSS")
#If, (SubTitle == "JavaScript")
#If

HTMLKeys := {"^XButton1" : "^]"
			, "^XButton2" : "^["
			, "^Numpad7" : "<link href=""./resources/css/style.css"" type=""text/css"" rel=""stylesheet"">"
			, "<!Left" : "{Home}"
			, "<!Right" : "{End}"
			, "^RButton" : "^/"}
Hotkey, If, (SubTitle == "HTML") ;case-sensitive comparison
bindHotkeysFromKeyMap(HTMLKeys)
Hotkey, If


CSSKeys := {"^NumpadDiv" : "border: 2px solid black;{Enter}"
		, "^Numpad8" : "resources\images\"
		, "^Numpad9" : "url(""../images/"");{Left 3}"}
Hotkey, If, (SubTitle == "CSS") ;case-sensitive comparison
bindHotkeysFromKeyMap(CSSKeys)
Hotkey, If

JavaScriptKeys := {"^l" : "console.log(){Left}"
				, "^Numpad7" : """"" : """""
				, "^Enter" : "(function() {{Enter}});"}
Hotkey, If, (SubTitle == "JavaScript") ;case-sensitive comparison
bindHotkeysFromKeyMap(JavaScriptKeys)
Hotkey, If

KeySet(msg) {
	SendInput, % msg
}

bindHotkeysFromKeyMap(KeyMap) {
	for keybind, command in KeyMap {
		bf  :=  Func("KeySet").bind(command)
		Hotkey, % keybind, % bf, On
	}
}

nextLayer() {
	static layers := ["HTML", "CSS", "JavaScript"]
	static index

	++index
	if (index > layers.MaxIndex()) {
		index := 1
	}

	return layers[index]
}

Esc::ExitApp, 0
MButton::
{
	SubTitle := nextLayer()
	ToolTip, % SubTitle, % (A_ScreenWidth // 2), % (A_ScreenHeight // 2)
	SetTimer, clearTooltip, -1000
return
}

clearTooltip:
{
	ToolTip
return
}

Code: Select all

; This would be automatic according to WinTitle
if the extension is shown somewhere in the title, you could SetTitleMatchMode, RegEx, whip up a regex to match and replace the Hotkey, If's with that
User avatar
Off Topic
Posts: 43
Joined: 07 Oct 2017, 20:57

Re: How do I switch between sets of hotkeys using Hotkey, If according to a variable's contents?

22 Apr 2018, 14:16

Code works perfectly, thanks so much! I had a bit more done on my end but your method was far better.
swagfag wrote:if the extension is shown somewhere in the title, you could SetTitleMatchMode, RegEx, whip up a regex to match and replace the Hotkey, If's with that
That's exactly what I was trying to do! Earlier I was using a Timer to check the Window title, but you got me curious with this suggestion and after a bit of digging I came up with a solution:

Code: Select all

SetTitleMatchMode, RegEx

HTMLKeys		 := {"^Numpad7" : "<link href=""./resources/css/style.css"" type=""text/css"" rel=""stylesheet"">"
					,"^Numpad8" : "This is HTML"}
Hotkey, IfWinActive, (?i).*\.html
If ErrorLevel {
	MsgBox % ErrorLevel
}
; Hotkey, If, (SubTitle == "HTML") ;case-sensitive comparison
bindHotkeysFromKeyMap(HTMLKeys)
Hotkey, If


CSSKeys	:=			 {"^Numpad7" : "border: 2px solid black;{Enter}"
					, "^Numpad8" : "This is CSS"}
Hotkey, IfWinActive, (?i).*\.css
; Hotkey, If, (SubTitle == "CSS") ;case-sensitive comparison
bindHotkeysFromKeyMap(CSSKeys)
Hotkey, If

JavaScriptKeys := 	{"^Numpad7" : "console.log(){Left}"
					, "^Numpad8" : "This is JavaScript"}
Hotkey, IfWinActive, (?i).*\.js
; Hotkey, If, (SubTitle == "JavaScript") ;case-sensitive comparison
bindHotkeysFromKeyMap(JavaScriptKeys)
Hotkey, If
It works! It'll really change things for me, thanks again.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb and 315 guests