How to Use AutoHotkey to Automate Anything in Windows (2024)

How to Use AutoHotkey to Automate Anything in Windows (1)

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.

How to Use AutoHotkey to Automate Anything in Windows (2)

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.

Also read: 15 Great AutoHotkey Scripts You Have to Try

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.

How to Use AutoHotkey to Automate Anything in Windows (3)

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:

#IfWinActive ahk_class Notepad #if

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.

How to Use AutoHotkey to Automate Anything in Windows (4)

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:

Click, X, Y ;For a left-click, or... Click, right, X, Y ;For a right-click.

How to Use AutoHotkey to Automate Anything in Windows (5)

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:

sendkeys, Make Tech Easier

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.

Send, {Space 5}Make Tech Easier{Tab 2}{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:

Send, ^A^C

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.

How to Use AutoHotkey to Automate Anything in Windows (6)

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:

#IfWinActive, ahk_exe firefox.exe ;Only activate if the window belongs to the firefox.exe application. !+M:: ;The script will activate whenever we press the combination Alt+Shift+M. Click, 1830, 140 Sleep, 50 ;Small delay, to make sure the search field has appeared on the screen. Send, {Clipboard}{Enter} ;Send the contents of the Clipboard to the search field and then press enter to initiate a search. return ;End the specific part of the script that, in this case, sets up a hotkey. #If ;Stop targeting the specific app\window.

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!

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

How to Use AutoHotkey to Automate Anything in Windows (7)

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.

  • Facebook
  • Tweet
How to Use AutoHotkey to Automate Anything in Windows (2024)
Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 6290

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.