If you enjoyed this site, please consider donating $3. Any amount is appreciated. Thanks!

AutoHotKey Basics

Xah Lee, 2009-08-25

This page is a very basic tutorial on using AutoHotKey.

AutoHotKey is a keyboard macro software for Microsoft Windows. It is free and Open Source. It lets you assign any keyboard shortcuts to launch programs, type text or keys, or even mouse clicks. It has a basic scripting language.

For example, you can define F5 to launch to a browser but switch to it if it's already running. You can also use it to define abbreviations, so pressing a key automatically types today's date, or your signature, or any other keypress.

Download AutoHotKey

First, you need to download and install it, here: http://www.autohotkey.com/download/. Just run the installer to install it. AutoHotKey runs on my machine without any problem. My machine is 64 bits Windows Vista.

Create And Running A Script

To create a AHK script, for example, save the following text into a file, and name it “test.ahk”:

; launch Notepad
Run Notepad

AutoHotKey script is not case sensitive. “Run” is the same as “run”.

To run the script, just double click it. It will launch Notepad, then the script will exit.

Here's how you create a keyboard shortcut to launch Notepad:

; assign Win+n to launch Notepad
#n::Run Notepad

Save the above in a file. Then, double click on it, it will run as a background process. Then, pressing “Win+n” will launch Notepad.

AutoHotKey System Notification Area

A running AutoHotKey script icon in Taskbar's System Notification Area.

Once you run the above script, it actually stays running as a background process. You can see it in your Taskbar's notification area. You can right click on the icon and pull a menu to Edit the script, Reload the script, or exit the script, and others. As long as the script is running, your hot key is available to you.

Launching Apps and Defining Hot Keys

Launching a app

; you can use “Run” to launch apps or url
Run Notepad       ; launch a app by name
Run "C:\Program Files (x86)\Internet Explorer\iexplore.exe" ; launch a app by path

Run C:\Users\xah\Documents\todo.txt ; launch a file
Run C:\Users\xah\Documents ; launch a folder

Run www.google.com    ; launch a url in default browser
; launching a app with a parameter
Run "C:\Program Files (x86)\emacs-23.1-bin-i386\emacs-23.1\bin\emacs.exe" "-Q"

Assign A Key To Launch Apps

; assign a hot key to launch a app
#n::Run Notepad     ; this means the Win+n
!n::Run Notepad     ; this means Alt+n
^n::Run Notepad     ; this means Ctrl+n

F6::Run Notepad     ; F6
^F6::Run Notepad    ; Ctrl+F6
^!n::Run Notepad    ; Ctrl+Alt+n

For the basic syntax for modifier keys, see http://www.autohotkey.com/docs/Hotkeys.htm. For complete list of key syntax, including function keys, keys on number pad, and special app launching keys and multimedia keys, see: http://www.autohotkey.com/docs/KeyList.htm.

Sending Text And Keystrokes

You can define a hot key, so that, when pressed, it sends some other typing or keystrokes.

; pressing Ctrl+Alt+s to type a signature
F8::
Send Best,{Enter}{Enter} Mary Jane
return

In the above, the “{Enter}” means the Enter key. When you press “Ctrl+Alt+s”, then it'll type:

Best,

 Mary Jane  

Here's some useful example on setting up keys that sends other keys.

; make the Pause/Break key close window. (does not work in all apps)
Pause:: Send ^w ; close window

; make F11 and F12 to switch tabs in Browsers
F11:: Send ^{PgUp} ; prev tab
F12:: Send ^{PgDn} ; next tab

; make Insert key minimize window
Ins:: WinMinimize, A ; minimize window

Launching or Switching To Browser

Suppose you want a hot key that launches you to a browser, but if it is already running, just switch to it. Here's how.

; F7 to launch or switch to FireFox

F7::
ifWinExist ahk_class MozillaUIWindowClass
{
  WinActivate
}
else
{
  Run "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
  WinWait ahk_class MozillaUIWindowClass
  WinActivate
}
return

Related essays:

2009-06
© 2009 by Xah Lee.