How to Write an AutoHotkey Script

autohotkeyAutoHotkey

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.

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

compile the 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.

autohotkey script being executed

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:

autohotkey guidelines

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.

autohotkey hotstrings

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.

Advertisements

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.