Conditional hotkey or include in AutoHotKey

Posted on

QUESTION :

Is there a way to define a Hotkey conditionally in AutoHotKey? I want to do different keyboard mappings for different machines with different physical keyboards.

This is what I want to do:

RegRead, ComputerName, HKEY_LOCAL_MACHINE, SystemCurrentControlSetControlComputerName, ComputerName
If ( ComputerName = BDWELLE-DIM8300 ) 
{
 #Include %A_ScriptDir%Mappings-BDWELLE-DIM8300.ahk
}

OR

RegRead, ComputerName, HKEY_LOCAL_MACHINE, SystemCurrentControlSetControlComputerName, ComputerName
If ( ComputerName = BDWELLE-DIM8300 ) 
{
 LWin::LAlt
    [more hotkey definitions that only apply to this machine]
}

but since AHK parses Hotkey definitions and #Include statements BEFORE interpreting If statements, the Hotkeys definitions (whether buried in an #Include or not) do not respect the If condition.

Thanks for pointing me to AutoHotKey_L!

Do you have a specific example of how to conditionally define a hotkey? The syntax is very confusing. Here’s what I’m trying (after having installed AutoHotKey_L.exe in place of AutoHotKey.exe):

RegRead, ComputerName, HKEY_LOCAL_MACHINE, SystemCurrentControlSetControlComputerName, ComputerName 
#If ( ComputerName = BDWELLE-DIM8300 ) 
LWin::LAlt 

but that doesn’t seem to work…

ANSWER :

This is an old question, I know, but I just had to do this this morning, and this is how I got it working in AutoHotkey_L:

#If %computername% = work-computer
^h::MsgBox Work
#If

#If %computername% != work-computer
^h::MsgBox Not at Work
#If

The key point is that your computer name is not quoted. That tripped me up for quite a while.

Hope it helps somebody out!

Look at the Hotkey command – it can create or modify HKs dynamically anywhere in a script.

Lifehacker presented an AHK script called AutoInclude that help you select which scripts to use on different computers. It accomplishes this by textually concatenating scripts into one file and then running that one file.

Disclaimer: I have not used it.

I solved this by creating separate scripts for each PC containing the PC-specific hotkeys, including a common script for the shared hotkeys, and using a separate script to launch the appropriate script.

Common.ahk

; Common hotkeys go here

[PC Name].ahk

#Include %A_ScriptDir%Common.ahk

; PC-specific hotkeys go here

Load.ahk

If FileExist(A_ScriptDir . "" . A_ComputerName . "".ahk"")

Leave a Reply

Your email address will not be published.