How to Write an AutoHotkey Script (2023)

How to Write an AutoHotkey Script (1)

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)

(Video) AHK is easy to use: AutoHotKey file tutorial

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 Chromereturn

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.

(Video) AutoHotkey - Beginners tutorial (All the basic functions)

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).

(Video) AHK for OSRS Tutorial #1 - The Basics!

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.


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.

(Video) FREE Tool to Automate Your Repetitive and Boring Tasks | AutoHotkey

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.


(Video) AutoHotKey Tutorial 1- Scripting Hotkeys/Macros

  • How to Hide Your Cursor While Typing in Windows 10 or 11
  • Gaming Keyboards vs. Keyboards: What’s the Difference?
  • What Is the Menu Key For? (and How to Remap It)
  • 5 Convenient Macros to Assign to Your Keyboard
  • How to Remap the Office Key on Your Keyboard
  • How to Use Caps Lock as a Modifier Key on Windows
  • Bing Chat Now Won’t Make You Wait for Access
  • How to Find Windows 11’s Blue Screen of Death Logs


1. AutoHotKey Tutorial 1 (The Basics to start writing scripts)
2. How to Code Your Own Macros | AHK
(Adam Schadow)
3. How to Write Autohotkey Scripts
4. How to Automate ANYTHING on your computer using AutoHotKey
(Online Business Tech)
5. AHK for Turking 101 - The process of writing a script.
6. How To Run AutoHotkey Scripts (Mini Tutorials #1)
Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated: 03/02/2023

Views: 5812

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.