AutoHotKey Basics

perm url with updates: http://xahlee.org/mswin/autohotkey.html

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 in Explorer. It will launch Notepad, then the script will exit.

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

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

Save the above in a file. Then, double click to run it. Then, you'll be able to press the “Win+n” key to 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

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

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 equivalent to pressing Ctrl+F4
Pause:: Send ^{F4} ; close window

; using the “/” and “*” keys on number pad to do
; prev/next tab in browser
NumpadDiv:: Send ^{PgUp} ; prev tab
NumpadMult:: Send ^{PgDn} ; next tab

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

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs