Categories
Self-organization

How to write an autohotkey script

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

AHK scripts are text files with a .ahk extension. 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 EXE file 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

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

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:

The first line defines a hotkey. The pound sign (#) is short for the Windows key and c is 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 Run command. The block is finished with a return at 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 .ahk file 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 right Shift. Take a look at the key 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

The green #IfWinActive is called a directive, 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 #IfWinActive only 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

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.

  • Sending input, whether it’s text or various button presses.
  • Moving the mouse around. In fact, AHK is sometimes erroneously flagged as cheat software for video games, since people have made fully functioning aimbots with it.
  • Clicking the mouse, with positioning relative to the current window.
  • Displaying dialog menus, complete with forms and input fields.
  • Moving windows around, adjusting size, and opening and closing.
  • Playing music.
  • Writing to the Windows Registry. Yes, really.
  • Modifying the contents of the Clipboard.
  • Reading and writing files. You can loop through files and run actions on each line. AHK can even write to .ahk files and adjust its own code.

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 If inside of action blocks. AHK has For loops, curly brace blocks, Try and Catch statements, and many others. You can access outside data from within the action block, and store it in variables or objects to use later. You can define custom functions and 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.

Just download, open in text editor, save and run

AutoHotKey is a beautiful tool. In an article published near the start of the year over at Help Desk Geek, I explained how to disable keys in Windows using AutoHotKey. However, that’s just one of the countless tricks that you can achieve with this software.

With just a few lines of code, you can create something that will change the way you use your keyboard and PC for years to come. For more than a decade now, I’ve had an ever-changing AutoHotKey script sitting in my Windows startup – without some of what it enables, I’d be completely lost.

How to write an autohotkey script

Let me share with you five of the most useful AutoHotKey scripts for everyday PC use. While I laid out a more detailed explanation for installing, setting up, and creating scripts using AutoHotKey in the aforementioned article, all you have to do is download the application, bring up a text editor, and save and run any of the following scripts to get them working immediately.

Repurpose The Function Keys

For many of us, the function keys (F1–F12) end up completely unused. Depending on your keyboard layout, the reach to this row of keys could be an uncomfortable trade-off compared to their functionality. For others, these keys may simply be useless.

My favorite thing to do with the function keys is to set them to launch programs that I often use but don’t often keep open. Notepad is a great example.

The above script sets the F1 key to launch Notepad in any modern version of Windows. As you can see, the file path supports both a direct path or one of Windows environment variables. Using environment variables is ideal if you use multiple different versions of Windows.

Use Special Characters

How to write an autohotkey script

As a massive fan of the em dash, it’s frustrating that the vast majority of keyboards don’t natively support it—so, let’s make them.

The above script will insert an em dash when the Alt + – keys are pressed. Alt is a great modifier to use for your hotkeys because it sees much less use compared to Shift and Ctrl.

Another solid idea is to bind ellipsis to Alt + ., which can be performed with the following one-liner:

As a writer, using AutoHotKey for easy access to punctuation marks saves me an incredible amount of time.

Control Your Volume

How to write an autohotkey script

Not everyone has a keyboard that supports multimedia keys, but that shouldn’t stop anyone from the joys of controlling their music with ease.

My favorite way of implementing this is by using the Shift + Page Up key to turn the volume up, Shift + Page Down key to turn the volume down, and Shift + Pause key to mute (toggleable).

Of course, there’s the chance that you may be on a keyboard where that layout isn’t very practical. You can simply change any of the key names above to your liking by checking out AutoHotKey’s list of keys.

Pin a Window On Top

How to write an autohotkey script

This might be my favorite AutoHotKey one-liner of them all. The ability to pin a window on top of others is one that can save you a major headache while working, trying to enjoy a movie, or plenty of other activities at your desk.

I find this one to be especially useful if you have a decent-sized monitor but not a dual-monitor setup. With the script above, that calculator will no longer get buried under all of your other windows! Just press Ctrl + Space to pin (or unpin) a window.

Search Google Instantly

How to write an autohotkey script

If you’re someone who uses your computer every single day, you probably spend more time than you notice searching Google for terms that you come across while talking to friends or surfing the web.

However, selecting text, copying it, opening a new tab, pasting the text into your address bar, and pressing the Enter key is an awfully long process. Why not make it simpler?

The script above allows the Ctrl + Shift + C hotkey to do all of that in a single hotkey as long as you’ve highlighted the text you want to search for. The Google Search page will be brought up in your default browser.

If you can’t pick just one of these scripts, there’s good news: All you have to do is paste each of them on a new line, and they’ll all work seamlessly together!

As long as you haven’t altered the hotkeys to create conflicts, using all five of the above scripts at once within a single AHK file should work perfectly fine.

Craig is a long-time writer, coder, and marketer with years of experience in the technology and gaming spaces. Since 2008, he’s worked remotely with some of the most notable publications in these industries, specializing in Windows, PC hardware and software, automation, and the like. Read Craig’s Full Bio

Introduction: How to Use Autohotkey and Write Basic Scripts for It.

How to write an autohotkey script

In this instructable, you will learn how to use autohotkey and how to make basic scripts.

Step 1: Downloading and Installing Autohotkey

First, you are required to go to here and download Autohotkey. Install it and start it up.

Step 2: Lets Learn How to Write Scripts! – Step 1

We are just going to do basic scripts, where we define a key or set of keys which we are going to press and then the action.
Right click on your desktop, and click New -> Autohotkey Script. Name it anything you want, and open it in your favorite text editor. I’m going to use notepad.
First, delete everything that is there. Feel free to edit the version, language, platform and author if you wish, but it is unnecessary.

In the script, we will start with something along the lines of this:
The up arrow means Ctrl, the ! means Alt and the w means w. Also, the windows key (#) and Shift (+) can be added here. The double colon (::) after the hotkey defines the set of keys to be the hotkey to start the script.
So,
means that the hotkey for this script is going to be Ctrl-Alt-W.

Now, onto the next step.

Step 3: Lets Learn How to Write Scripts! – Step 2

Ok, so we have defined the hotkey, we have to tell it to do something when it is pressed. So far we have:
There are many different commands that we can enter. Four simple ones are:
Send – which can send keystrokes,
Wait – which makes the program wait before commands
Run – which can run applications or files
MsgBox – which makes a message box pop up on your screen

We are going to use the command send. After we type a command, we always add a comma (,) and then a space after it. So, to add to what we already have I will add “Send, “.
Now, obviously we want it to send some keystrokes. The same form of shortening applies here, just like with the hotkeys used in step 2 of the ‘ible.
Ctrl (up arrow)
Alt !
Shift +
Also can be used to make the ‘enter’ keystroke.

Lets make it say hello world!
Now, lets move onto the next step to learn how to finish off a script.

Step 4: Lets Learn How to Write Scripts! – Step 3

Ok, so far we have:
To finish off this script, we are required to end it with ‘return’. This means that you will be able to use the script over and over again. If you do not enter return at the end, you will only be able to use the script once, before resetting the program to use it again, etc.

So, our hello world script is finished!

Step 5: Running Your New Script

Save your newly made script to whatever location you wish. Make sure that it has .ahk on the end. .ahk is the suffix that is added to autohotkey scripts. Now, double click on the script to run it, and your off! Every time you press Ctrl-Alt-w, it will type out Hello World! for you!

Step 6: Other Useful Scripts

There are many other useful scripts available to download off the internet.
For fps gamers, there is a rapid fire script, so that when you hold down the mouse button, the gun you are using will rapid fire, even if its not an automatic weapon.

The rapidfire script is:
(By the way, i did not code this script. It is written by Byro on autohotkey.com)
To activate this script, just press the insert button on your keyboard!

Thank you for taking the time to read my instructable. I hope you had fun and continue to find out about more about Autohotkey coding!

Be the First to Share

Did you make this project? Share it with us!

Recommendations

How to write an autohotkey script

How to write an autohotkey script

How to write an autohotkey script

How to write an autohotkey script

Battery Powered Contest

How to write an autohotkey script

Plastic Contest

How to write an autohotkey script

Make it Real Student Design Challenge

How to write an autohotkey script

3 Discussions

How to write an autohotkey script

10 years ago on Step 6

Script could hav been more legible

How to write an autohotkey script

can’t remember where I got this, but this works too: $x:: Loop < if not GetKeyState("x", "P") break sleep 10 Click >return if you want rapid fire, hold X. very useful. btw, nice instructable. We need more AHK instructables.

Learn how to write a simple AHK script

Create the AHK script file.

There are two simple ways to do this:

  • Right click your desktop or in a folder and choose New > AutoHotkey Script. You would then give the script a suitable name.
  • Open a new notepad file and save the file with the .ahk file extension. So you would open notepad, choose File > Save As, and then save the file as “yourtitlehere.ahk”.

Open the script file for editing.

If your script it not yet open, right click the script file and click Edit Script.

With the script file open, start coding.

We will start with a very simple script. Copy the following into the script file:

Script Breakdown: ^t:: is denoting the key you want to bind your macro to. In this case it is the key combination control + t. In AHK ^ stands for the control key. Sendinput is a command that tells AHK to send the following sequence of keys. In this case it will send “This is my very first script” Finally, the return command tells AHK that it has reached the end of the code for the current macro (AHK scripts can contain several hotkeys/macros in a single file and if you forget the return command AHK will immediately start running the next macro in the script without stopping — which can wreak some havoc).

Double click your script file (outside of the editor) to start the script.

You may notice an AHK icon pop up in your system tray at the bottom right of your computer. When you edit a script you can right click this icon and choose “reload script” to activate any changes you have made since first running the script. If you don’t reload the saved changes will have no effect.

Place your cursor somewhere text can be written and press the hotkey combination Control+T.

You should notice “This is my very first script” being written every time you use this hotkey. Congratulations, you have written your first AHK script!

Mturk Guide by TSolo315. 2019 В© All Rights Reserved.

How to write an autohotkey script

Hi, I tried, and did some test on my own, but I still cant figure it out, so i need a little help. I am try to spam F1-F12 key in a program (game). I am going to do it in 2 windows. However, i like to find out how i can spam in 2 windows. The Script must run individually in that window with different combination. For, explain. Window 1, Will spam F1,F3,F5,F6,F7 while Window 2 will spam F2,F1,F6,F7. and i am plan to do it in different window. Here what i did so far

How to write an autohotkey script

You can use ControlSend to acheive this (read the documentation on it).

Details are a pain in the butt aren’t they? But computers and computer programs love them. So, with that in mind, here is a script that you will need to fill in the details on that will do exactly what you want. IF you can give the script the right details.

Window 1, Will spam F1,F3,F5,F6,F7 while Window 2 will spam F2,F1,F6,F7

And, yes, i realize you have different sleeps for the keys, you can modify the code to account for that if you like.

Never assume evil intent when simple ignorance will suffice. Ignorance is an eventually curable condition with the right education. Evil intent, however, is another matter entirely. Scripts are much like children. Simple to conceive. Difficult, expensive, and time-consuming to raise. Often do the opposite of what you expect them to. Require frequent “correction”. And once they leave home you can’t control them anymore. But you love them anyway.

How to write an autohotkey script

  • Members
  • 2 posts
  • Last active: Apr 20 2013 11:34 PM
  • Joined: 17 Mar 2013

Hi ,Jade thank you for repying. I peroanlly remake the script after a bit of reading and youtube.

I open an AHK scripts for each window, and I add on abit on top what i learn.

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

AHK scripts are text files with a .ahk extension. 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 EXE file 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

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

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:

The first line defines a hotkey. The pound sign (#) is short for the Windows key and c is 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 Run command. The block is finished with a return at 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 .ahk file 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 right Shift. Take a look at the key 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

The green #IfWinActive is called a directive, 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 #IfWinActive only 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

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 If inside of action blocks. AHK has For loops, curly brace blocks, Try and Catch statements, and many others. You can access outside data from within the action block, and store it in variables or objects to use later. You can define custom functions and 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

How to write an autohotkey scriptAutoHotkey

AutoHotkey is a fantastic but complicated software. Initially, it was intended to link custom keyboard shortcuts to different actions, but it is now a full suite of Windows automation.

AHK is not particularly difficult to learn for new users, because the general concept is quite simple, but it is a complete and complete programming language. The syntax is much easier to understand if you have programming experience or if you know the concepts.

Installing and Using AutoHotkey

The installation process of AutoHotkey is simple. Download the fitter from the official website and launch it. Choose “Quick Install”. Once the software is installed, you can right-click anywhere and select New> AutoHotkey Script to create a new script.

How to write an autohotkey script

AHK scripts are text files with an .ahk extension. If you right-click on it, you will have several options:

“Run Script” will load your script with the AHK runtime.
“Compilation Script” will group it with an AHK executable to create an EXE file that you can execute.
“Edit Script” will open your script in your default text editor. You can use Notepad to write AHK scripts, but we recommend that you use SciTE4AutoHotkey, an editor for AHK that supports syntax highlighting and debugging.

How to write an autohotkey script

When a script is running (whether it’s an EXE file or not), it runs in the background in the Windows notification area, also known as the notification area. Look for the green icon with an “H” on it.

To quit, pause, reload, or edit a script, right-click the notification icon and select an appropriate option. The scripts will continue to run in the background until you leave them. They also disappear when you log out of Windows or restart your PC, of ​​course.

How to write an autohotkey script

How does AutoHotkey work?

AHK basically does one thing: link actions to keyboard shortcuts. There are many different actions, shortcut key combinations, and control structures, but all scripts will work on the same principle. Here’s a basic AHK script that launches Google Chrome every time you press Windows + C:

#c ::
Run Chrome
return

The first line defines a keyboard shortcut. The pound sign (#) is the abbreviation for the Windows key and it is the C key on the keyboard. After that, there is a double semicolon (: 🙂 to indicate the beginning of an action block.

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

Just like that, you have defined a simple key-to-action mapping. You can place as many as you want in an .ahk file and set it to run in the background, always looking for keyboard shortcuts to remap.

Hotkeys and modifiers

You can find a complete list of AHK modifiers in the official documentationbut we will focus on the most useful (and cool) features.

The modifier keys all have shortcuts to one character. For example, # ! ^ + are Windows, Alt, Control and Shift, respectively. You can also tell the difference between Alt, Control and Shift left and right with the button modifiers, which opens up a lot of room for extra keyboard shortcuts. For example, + is right Shift. look at the list of keys for everything you can reference. (Spoiler: you can reference just about every key, you can even reference other input devices small extension).

You can combine as many keys as you want in one hotkey, but you’ll soon miss key combinations to remember. This is where the modifiers, which allow you to do more crazy things, come into play. Let’s describe an example. AHK documentation:

How to write an autohotkey script

The green #IfWinActive calls a directive and applies additional context to the shortcut keys physically beneath it in the script. Any shortcut key that follows will only be triggered if the condition is true and you can group multiple shortcut keys under a single directive. This directive will not change until you reach another directive, but you can reset it with a blank #If (and if that sounds like a hack, welcome to AHK).

The directive here checks if a specific window is open, defined by ahk_class Notepad. When AHK receives the “Win + C” entry, it triggers the action under the first #IfWinActive only if the directive returns true, then checks the second if it does not. AHK has a lot of guidelines, and you can find them all in the docs.

AutoHotkey also ropes, which function as keyboard shortcuts, except that they replace a whole string of text. This is similar to the operation of automatic correction. In fact, there is a automatic correction script for AHK – but supports any AHK action.

How to write an autohotkey script

The string matches the string only if it is typed exactly. It will automatically delete the corresponding text to replace the character string, too, although this behavior can be adjusted.

actions

An action in AHK is anything that has an external effect on the operating system. AHK has a lot of actions. We can not possibly explain them all, so we will choose some useful ones.

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

Link everything with control structures

AHK would not be what it is without all the control structures that make it Turing-complete.

In addition to the #If directives, you also have access to Yes inside the action blocks. AHK has For loops, accolade blocks, Try and catch reports, and many others. You can access external data from the action block and store it in variables or objects to use later. You can define custom functions and labels. Really, everything you could easily do in another programming language that you can probably do in AHK with a little headache and a look in the documentation.

For example, imagine that you have a tedious and repetitive task that requires you to click more than one button in a row and wait for the server to answer before remaking it to infinity. You can use AHK to automate this. You want to set a few loops to move the mouse to specific locations, click, then go to the next point and click again. Add some waiting statements to avoid breakage. You could even try to read the color of the pixels on the screen to determine what is happening.

One thing is certain: your script will probably not be pretty. But AutoHotkey either, and it does not matter.

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

AHK scripts are text files with a .ahk extension. 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 EXE file 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

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

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:

The first line defines a hotkey. The pound sign (#) is short for the Windows key and c is 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 Run command. The block is finished with a return at 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 .ahk file 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 right Shift. Take a look at the key 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

The green #IfWinActive is called a directive, 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 #IfWinActive only 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

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 If inside of action blocks. AHK has For loops, curly brace blocks, Try and Catch statements, and many others. You can access outside data from within the action block, and store it in variables or objects to use later. You can define custom functions and 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.

Understanding How AutoHotkey Works and Designing Robust Applications

(EPUB format for iPad, iPhone, Android, and other devices and computers)

AutoHotkey Beginner’s Bundle (EPUB, MOBI, and PDF)

How to write an autohotkey script

Don’t know what books or which format is best for you? Get these beginning books at one special price! All Starter AutoHotkey books! All three e-book formats!

  • Price: $33,90
  • Sale Price: $19,95

AutoHotkey Essentials Bundle (EPUB, MOBI, and PDF)

How to write an autohotkey script

In this Essentials Bundle, you get both the Starter and Intermediate Bundles at one special price! To add the Motley AutoHotkey Tips book, see the Library Mega Bundle.

  • Price: $65,87
  • Sale Price: $34,95

AutoHotkey Library Mega Bundle (EPUB, MOBI, and PDF)

How to write an autohotkey script

Don’t know what books or which format is best for you? Get everything at one special price! All 10 AutoHotkey books! All three e-book formats!

  • Price: $93,86
  • Sale Price: $49,95

Beginning Tips for Writing AutoHotkey Scripts (EPUB)

How to write an autohotkey script

Understanding How AutoHotkey Works and Designing Robust Applications (EPUB format for iPad, Android, and computers) “As a new AutoHotkey scriptwriter, this may be the most important book you can read.”

Beginning Tips for Writing AutoHotkey Scripts (MOBI for Kindle)

How to write an autohotkey script

Understanding How AutoHotkey Works and Designing Robust Applications (MOBI for Amazon Kindle) “As a new AutoHotkey scriptwriter, this may be the most important book you can read.”

Beginning Tips for Writing AutoHotkey Scripts (PDF for Print)

How to write an autohotkey script

Understanding How AutoHotkey Works and Designing Robust Applications (PDF format for printing) “As a new AutoHotkey scriptwriter, this may be the most important book you can read.”

Beginning Tips for Writing AHK Scripts (EPUB, MOBI, PDF Bundle)

How to write an autohotkey script

Understanding How AutoHotkey Works and Designing Robust Applications (EPUB, MOBI and PDF Bundle.) “As a new AutoHotkey scriptwriter, this may be the most important book you can read.”

Also see these AutoHotkey books in individual formats: