Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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, System\CurrentControlSet\Control\ComputerName, ComputerName
If ( ComputerName = BDWELLE-DIM8300 ) 
{
 #Include %A_ScriptDir%\Mappings-BDWELLE-DIM8300.ahk
}

OR

RegRead, ComputerName, HKEY_LOCAL_MACHINE, System\CurrentControlSet\Control\ComputerName, 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, System\CurrentControlSet\Control\ComputerName, ComputerName 
#If ( ComputerName = BDWELLE-DIM8300 ) 
LWin::LAlt

but that doesn't seem to work...

share|improve this question

5 Answers 5

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!

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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")
    Run "autohotkey.exe" %A_ScriptDir%\%A_ComputerName%.ahk
Else
    Run "autohotkey.exe" %A_ScriptDir%\Common.ahk
share|improve this answer

You may want to give Autohotkey L a shot. Among other things, it has a #If statement, which works just like #IfWinActive, but for arbitrary code.

share|improve this answer
    
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, System\CurrentControlSet\Control\ComputerName, ComputerName #If ( ComputerName = BDWELLE-DIM8300 ) LWin::LAlt but that doesn't seem to work... –  Bowen Dec 18 '09 at 17:43
    
I think you have to end it with another #if, like #IfWinActive, but I've never actually used AHKL, sorry :( –  Phoshi Dec 18 '09 at 18:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.