How to Write an AutoHotkey Script (2024)

By Anthony Heddings

Learn AutoHotkey, and automate your daily tasks.

How to Write an AutoHotkey Script (1)

Quick Links

  • Installing and Using AutoHotkey
  • How Does AutoHotkey Work?
  • Hotkeys and Modifiers
  • Actions
  • Tying it All Up With Control Structures

AutoHotkey is a fantastic but complicated piece of software. It was initially intended to rebind custom hotkeys to different actions but is now a full Windows automation suite.

AHK isn't particularly hard to learn for new users, as the general concept is fairly simple, but it is a full, Turing-complete programming language.You will pick up the syntax much easier if you have a programming background or are familiar with the concepts.

Installing and Using AutoHotkey

AutoHotkey's installation process is straightforward. Download the installer from the official website and run it. Choose "Express Installation." After you've installed the software, you can right-click anywhere and select New > AutoHotkey Script to make a new script.

How to Write an AutoHotkey Script (2)

AHK scripts are text files with a .ahkextension. If you right-click them, you'll get a few options:

  • "Run Script" will load your script with the AHK runtime.
  • "Compile Script" will bundle it with an AHK executable to make an EXEfile you can run.
  • "Edit Script" will open your script in your default text editor. You can use Notepad to write AHK scripts, but we recommend using SciTE4AutoHotkey, an editor for AHK which supports syntax highlighting and debugging.
How to Write an AutoHotkey Script (3)

While a script is running---whether it's an EXE or not---you'll find it running in the background in the Windows notification area, also known as the system tray. Look for the green icon with an "H" on it.

To exit, pause, reload, or edit a script, right-click the notification icon and select an appropriate option.Scripts will continue to run in the background until you exit them. They'll also go away when you sign out of Windows or reboot your PC, of course.

How to Write an AutoHotkey Script (4)

How Does AutoHotkey Work?

At its core, AHK does one thing---bind actions to hotkeys. There are a lot of different actions, hotkey combinations, and control structures, but all scripts will operate on the same principle. Here's a basic AHK script that launches Google Chrome whenever you press Windows+C:

#c:: 

Run Chrome

return

The first line defines a hotkey. The pound sign (#) is short for the Windows key and cis the C key on the keyboard. After that, there's a double colon (::) to signify the start of an action block.

The next line is an action.In this case, the action launches an application with the Runcommand. The block is finished with a returnat the end. You can have any number of actions before the return.They will all fire sequentially.

Just like that, you've defined a simple key-to-action mapping. You can place as many of these as you'd like in a .ahkfile and set it to run in the background, always looking for hotkeys to remap.

Hotkeys and Modifiers

You can find a full list of AHK's modifiers in official documentation, but we'll focus on the most useful (and cool) features.

Modifier keys all have single character shorthands. For example, # ! ^ + are Windows, Alt, Control, and Shift, respectively. You can also differentiate between left and right Alt, Control, and Shift with the < and > modifiers, which opens up a lot of room for extra hotkeys. For example, <! is left Alt and >+ is right Shift. Take a look at thekey list for everything you can reference. (Spoiler: You can reference nearly much every key. You can even reference other non-keyboard input devices with a small extension).

You can combine as many keys as you'd like into one hotkey, but you'll soon run out of key combinations to remember. This is where modifiers, which let you do crazier things, come in. Let's break down an example from the AHK docs:

How to Write an AutoHotkey Script (5)

The green #IfWinActive is called adirective, and applies additional context to hotkeys physically under it in the script. Any hotkey after it will only fire if the condition is true, and you can group multiple hotkeys under one directive. This directive won't change until you hit another directive, but you can reset it with a blank #If (and if that seems like a hack, welcome to AHK).

The directive here is checking if a specific window is open, defined by ahk_class Notepad. When AHK receives the input "Win+C," it will fire the action under the first #IfWinActiveonly if the directive returned true, and then check the second one if it didn't. AHK has a lot of directives, and you can find all of them in the docs.

AutoHotkey also has hotstrings, which function like hotkeys except replacing a whole string of text. This is similar to how autocorrect works---in fact, there's an autocorrect script for AHK---but supports any AHK action.

How to Write an AutoHotkey Script (6)

The hotstring will match the string only if it's typed exactly. It will automatically remove the matched text to replace the hotstring, too, although this behavior can be adjusted.

Actions

An action in AHK is anything that has an outside effect on the operating system. AHK has a lot of actions. We can't possibly explain all of them, so we'll pick out some useful ones.

Most of these actions will also have information-oriented commands associated with them. For example, you can write to the clipboard, but you can also get the contents of the Clipboard to store in a variable and run functions when the clipboard changes.

Tying it All Up With Control Structures

AHK wouldn't be what it is without all of the control structures that make it Turing-complete.

In addition to the #If directives, you also have access to Ifinside of action blocks. AHK has Forloops, curly brace blocks, Tryand Catch statements, and many others. You can access outside data from within the action block, and store it in variablesor objectsto use later. You can define custom functionsand labels. Really, anything you could do easily in another programming language you can probably do in AHK with a bit of a headache and a look through the docs.

For example, imagine you have a boring, repetitive task that requires you to click multiple buttons in a row and wait for a server to respond before doing it over again ad infinitum. You can use AHK to automate this.You'd want to define a few loops to move the mouse to specific locations, click, and then move to the next spot and click again. Throw in a few wait statements to make it not break. You could even try to read the color of pixels on screen to determine what's happening.

One thing's for certain---your script probably won't be pretty. But neither is AutoHotkey, and that's okay.

How to Write an AutoHotkey Script (2024)
Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 6278

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.