Writing Hotkeys

A hotkey is a key, or a combination of keys, that runs code instead of typing a character. Below is the shortest possible path from an empty file to a shortcut that actually does something.

The shape of a hotkey

Name the key on the left, put two colons after it, and place the action on the right:

#n::Run "notepad.exe"

That single line means: while Windows key is held, pressing n starts Notepad. The # symbol is a modifier prefix. The set you will use most often is:

Stack them freely. ^!t is Ctrl+Alt+T, and ^+#s is Ctrl+Shift+Win+S.

More than one line

When the action needs several steps, open a block with braces. Every statement inside runs in order when the hotkey fires:

^!t::
{
    Run "notepad.exe"
    WinWaitActive "ahk_exe notepad.exe"
    Send "Meeting notes{Enter}{Enter}"
}

The script waits for the window to be ready before typing, which avoids sending keystrokes into whatever had focus a moment earlier.

Shortcuts that expand as you type

A hotstring watches for a short trigger and swaps it for longer text. Wrap the trigger in a pair of colons on both sides:

::eod::end of day
::addr::1200 Harbor Street, Suite 14

The replacement appears once you finish the trigger with a space, a period or Enter.

Swapping one key for another

Remapping is written the same way, with a key on both sides. This turns the rarely wanted CapsLock into a second Ctrl:

CapsLock::Ctrl

Mouse buttons work too, so XButton1::Enter puts Enter under a thumb button.

Keeping a hotkey out of the way

A global shortcut can collide with an application that already uses it. Restrict a hotkey to a single program with a context directive, and everything after it applies only inside that window:

#HotIf WinActive("ahk_exe explorer.exe")
^b::Send "^c"
#HotIf

The bare #HotIf at the end closes the context so later hotkeys stay global again.

Running what you wrote

Save the file with an .ahk extension and double-click it. A tray icon appears while the script is loaded. Edit the file, then choose Reload This Script from the tray menu to pick up your changes without restarting anything else.

If a hotkey seems dead, check three things first: another program may already own the combination, the script may have exited, or the target window may need administrator rights that the script does not have.