How to Use AutoHotkey to Automate Anything in Windows

Automate Windows With Ahk Featured

AutoHotkey is no stranger to Make Tech Easier, but in the past we mainly talked about predefined solutions. The thing is, AutoHotkey is not (only) a platform for running scripts someone else made; it’s primarily a scripting language allowing you to build your own solutions for your particular needs and to automate anything you do on your Windows desktop. Covering its complete functionality would require a series of books since AutoHotkey has evolved into a full programming language and now even allows you to create apps with it.

For this tutorial, we start with the bare basics that will get you started in turning any multi-step process into a single keystroke.

Installation

Download and install AutoHotkey from its official site. Choose the current version. Ignore the other two options – “V2” is a new version, incompatible with existing scripts and with a slightly different syntax, while “V1.0 Deprecated” is old and restricted.

Make a blank script

After its installation, AutoHotkey will have registered as the default app for files with the AHK extension. That’s why they work as executables – AutoHotkey parses them in realtime and executes their contents. Actually, though, AHK files are scripts you can open in any text editor.

Automate Windows With Ahk New Script

Create a new folder for your first AutoHotkey script wherever you wish, enter it, right-click and create a “New -> AutoHotkey Script” from the new option in your right-click menu. Give it whatever name you wish.

Choose how you’ll edit your scripts

Instead of double-clicking the AHK file you created like you’d do to run a pre-made script, right-click on it and select Edit. You can edit AHK files in any text editor, but since scripting is a lite version of programming, it’s better if you use a program made for that purpose. Anything from Atom to Sublime Text or VS Code will do. If you don’t have any such tool installed, and since your first scripts will be simple, you can even use Windows’s Notepad.

For those wondering, I personally use all those tools for other purposes (like HTML, CSS, and some very basic JS, PHP, and Python), but for editing AHK scripts, I find myself returning to the popular Notepad++.

Target specific apps or windows

Before we start, let’s first check out the program we wish to make a script for.

You can create global scripts in AutoHotkey that will work everywhere, in every app and the Windows desktop, but you can also target apps. This allows you to have, for example, the same shortcut doing different things in two different programs.

Automate Windows With Ahk Target Apps And Windows

Your script will be pre-populated with some basic settings. Don’t modify them – type after those. Press Enter once or twice.

Enter this in your script:

This tells AutoHotkey that anything following “IfWinActive” should only work in a window with a Notepad class (ahk_class Notepad). The #If after it closes the section so that anything following it will not be confined to windows with a Notepad class.

Meet Window Spy

Run your empty script to have the AHK icon appear in the Windows tray. Right-click on it and choose Window Spy from the menu that pops up. Now, whenever you click on any other window, AutoHotkey’s Window Spy will present you information about it.

Automate Windows With Ahk Windows Spy

At the very top you’ll be able to see the different ways you can target it based on its title, class, and executable. If you run Firefox and, with Window Spy on your screen, click on its window. You’ll see that its executable is “firefox.exe.” Revise your script swapping the Notepad reference for #IfWinActive ahk_exe firefox.exe. You could target any other window the same way – use Window Spy to investigate its title, class, or executable and target any of them. Read more about it here. The short version is:

  • You can target windows by their title by using #IfWinActive accompanied by the title.
  • You can target windows by their class by using #IfWinActive ahk_class, followed by the class Window Spy recognized.
  • You can target programs by their executable with #IfWinActive ahk_exeand the executable’s filename. Note that this way,you target all the program’s windows, and not a specific one.

Check your mouse coordinates

With Window Spy active, turn your attention to the “Mouse Position” group of variables. Those show you the exact location of the mouse.

  • Absolute shows the location in relation to your whole screen’s resolution.
  • Relative shows the location within the active window without being affected by its placement on the screen. This is the default and, in most cases, what you’ll probably use.
  • Client is the same as relative but doesn’t take into account any windows decoration – like the title bar or borders.

Replicating mouse movements and clicks

You now know how to create a new AHK script and target a specific window, but what about the mouse coordinates we talked about? To move your mouse to coordinates X, Y, as read by Window Spy, use MouseMove, X, Y ;. This moves the mouse to position X, Y. Remember: the relative coordinates are the default.

Also, note how you can add comments in your code after the ; character. For multi-line comments, start with “/” and end with “/” – without the quotation marks. Moving the mouse is one thing. To click, you don’t have to move the mouse in a specific position beforehand. Just use:

Automate Windows With Ahk Spying Mouse Coordinates

Note that if you don’t enter any coordinates, the click will still happen where the mouse cursor is.

Replicate any keyboard keypress

We left the most important part of the equation for last: sendkeys. This is the command used in AutoHotkey to replicate any keypresses. By using it, you can direct AutoHotkey to send any string of keypresses to any program, just like if you had pressed the keys yourself. For example, the following:

tells AutoHotkey to send the keypresses that make up the phrase “Make Tech Easier.” You can use any letters or numbers. Special keys have their own shortcodes and are surrounded by brackets. Some of them are:

  • {Tab}
  • {Shift}
  • {Control} or {Ctrl}
  • {Alt}
  • {F1 – F12)
  • {LWin}{RWin} Left and right Windows key, respectively
  • {Enter}
  • {Space}
  • {Backspace}
  • {Delete}
  • {Up}{Down}{Left}{Right} ;Cursor keys Up, Down, Left and Right
  • {Home}
  • {End}
  • {PgUp}{PgDown} ;Page Up and Page Down
  • {Volume_Up}{Volume_Down}{Volume_Mute} ;Media control shortcuts for controlling sound volume

You can also use numbers to simulate repeated keypresses. The following will make AutoHotkey send five spaces to any active window, just like if you press the space bar five times. Then, type “Make Tech Easier,” followed by two presses of the Tab key and one of Enter.

AutoHotkey also sets four symbols as modifiers that help in sending shortcut key combinations to the active program. Those affect only the very next character following them and are:

  • ! for Alt
  • + for Shift
  • ^ for Control
  • # for Windows Key

The following would send to the active window the combination Ctrl + A to, say, select all text, then Ctrl + C to copy it to the Clipboard:

Make your first script

Let’s see how everything we saw up to now translates to a real-world example by creating a script that will add a shortcut to Make Tech Easier’s search function. By pressing a hotkey, the mouse will move and click the search icon on our site. Then enter whatever is in the Clipboard in the search field and send an “Enter” to start a search.

Automate Windows With Ahk Your First Script

We start by checking with Window Spy and find that the coordinates for the search icon, when Firefox’s window is maximized in a 1920 x 1080 screen, are close to X=1835 and Y=135. Those numbers vary because the search icon is larger than a single pixel, so X = 1830 and Y = 140 would also work since the cursor is still pointing at the icon when at those coordinates. Knowing this, we can write:

Yes, it is that simple, almost like writing in plain English. And, if you think about it, this allows you to automate pretty much anything you do on your computer.

  • Would you like to map launching five applications or Ryu’s “Hadouken” move in Street Fighter to a single keystroke?
  • To move the mouse cursor to a specific point in a window and click 50 times in inhuman rapid succession?
  • To make Caps Lock more useful by turning it into the middle-click your mouse lacks?

This is all doable in precisely the same way! Think about all the procedures you repeat every day, break them down to the parts of their sum and replicate each part with AutoHotkey to vastly simplify how you use your computer and boost your productivity. Go forth and automate!

Odysseas Kourafalos Odysseas Kourafalos

OK's real life started at around 10, when he got his first computer - a Commodore 128. Since then, he's been melting keycaps by typing 24/7, trying to spread The Word Of Tech to anyone interested enough to listen. Or, rather, read.

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.