Different hotkeys depending upon host loaded into

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Different hotkeys depending upon host loaded into

Post by roysubs » 29 Sep 2018, 16:42

I think this should be very simple. I only want to define ^+i etc depending upon the hostname, but I get errors.
Error: Duplicate hotkey. This error makes no sense from the perspective of any other programming language.
Appreciate if you can show me how to fix this? Thanks.

Code: Select all

^+c::Run, Explorer /n`,/e`,C:\
^+d::Run, Explorer /n`,/e`,D:\
if (%computername% = "Host1") {
	^+i::Run, Explorer /n`,/e`,"\\hp1\drives\Drive-D\0 Cloud\Dropbox"
	^+o::Run, Explorer /n`,/e`,"\\hp1\drives\Drive-D\0 Cloud\OneDrive"
	^+p::Run, Explorer /n`,/e`,"\\hp1\drives\Drive-D\0 Cloud\Google Drive"
}
if (%computername% = "Host2") {
	^+i::Run, Explorer /n`,/e`,"D:\0 Cloud\Dropbox"
	^+o::Run, Explorer /n`,/e`,"D:\0 Cloud\OneDrive"
	^+p::Run, Explorer /n`,/e`,"D:\0 Cloud\Google Drive"
}

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

Re: Different hotkeys depending upon host loaded into

Post by jeeswg » 29 Sep 2018, 16:48

You probably want something like this:

Code: Select all

^+c::Run, Explorer /n`,/e`,C:\
^+d::Run, Explorer /n`,/e`,D:\
#If (computername = "Host1")
	^+i::Run, Explorer /n`,/e`,"\\hp1\drives\Drive-D\0 Cloud\Dropbox"
	^+o::Run, Explorer /n`,/e`,"\\hp1\drives\Drive-D\0 Cloud\OneDrive"
	^+p::Run, Explorer /n`,/e`,"\\hp1\drives\Drive-D\0 Cloud\Google Drive"
#If (computername = "Host2")
	^+i::Run, Explorer /n`,/e`,"D:\0 Cloud\Dropbox"
	^+o::Run, Explorer /n`,/e`,"D:\0 Cloud\OneDrive"
	^+p::Run, Explorer /n`,/e`,"D:\0 Cloud\Google Drive"
#If
You use #If / #IfWinActive etc to make context-sensitive hotkeys/hotstrings. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Re: Different hotkeys depending upon host loaded into

Post by roysubs » 30 Sep 2018, 06:47

Ah ha! Thanks jeeswg, AHK syntax is a bit odd to say the least, though I guess it has some special functions to make interacting with open windows easy so for what I need it for, it's really versatile.

One more thing, I see that you don't put %computername%, so does AHK automatically recognise all environment variables dynamically as part of how it functions? i.e. if an environment variable changes after the script is loaded, it will always check that, and if a new environment variable is created, it will be aware of that?

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

Re: Different hotkeys depending upon host loaded into

Post by jeeswg » 30 Sep 2018, 06:57

- Whether environment variables are assigned or not depends on whether #NoEnv is on or not in AHK v1.
- AFAIK environment variables are never assigned automatically in AHK v2, so I would use A_ComputerName, otherwise EnvGet.
- I looked at the source code for A_ComputerName and EnvGet, they use Winapi functions to retrieve the values each time, suggesting that each time you use them, you will get the up-to-date value. I don't know about 'computername' etc, perhaps they are only assigned when the script starts (or perhaps they are reassigned if you make the variable blank and use it again). You could test this of course, and report back!
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Re: Different hotkeys depending upon host loaded into

Post by roysubs » 01 Oct 2018, 04:49

hmm, I see that the following all display, but when I turn on #NoEnv, nothing displays, so that disables all of the AHK built in resolved A_ComputerName, and from Environment Variables like %ComputerName%. I doubt it would have much impact on performance to leave #NoEnv on (maybe a couple of milliseconds) so I guess I'll just stick with Env resolution turned on for everything.

Code: Select all

; #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
Msgbox, %A_ComputerName% %A_OSVersion% %A_ScriptFullPath%
Msgbox, %ComputerName% %OS% %Path% 
Msgbox, % ComputerName OS Path
I did a test on dynamic environment variable loading, and it seems it does not, so the value it picks up when the script is first run is not updated - below displays the variables, then during the 10 second sleep I change the Path value in Control Panel, then after this the variables are displayed again, but Path is not updated.

Code: Select all

Msgbox, %A_ComputerName% %A_OSVersion% %A_ScriptFullPath%
Msgbox, %ComputerName% %OS% %Path% 
Msgbox, % ComputerName OS Path
Sleep, 10000
Msgbox, %A_ComputerName% %A_OSVersion% %A_ScriptFullPath%
Msgbox, %ComputerName% %OS% %Path% 
Msgbox, % ComputerName OS Path

Post Reply

Return to “Ask for Help (v1)”