Shortcuts allow us to be much more productive with our computers. At a press of a key combination, we have instant access to functions we’d otherwise have to hunt down on toolbars, menus, and submenus. However, not all apps come with shortcut keys, and even if they do, they may not do the things you want them to.
In this tutorial, we make use of AutoHotkey to write a simple script to enable different shortcuts depending on the active window’s name. This way, you can easily create keyboard shortcuts for almost every app.
Creating a basic script
If you haven’t installed AutoHotkey, download the installer and install it on your Windows computer.
Open your favorite file manager and create a folder to keep your autohotkey scripts. While inside the folder, right click and choose “New -> AutoHotkey Script,” then name your script.
Right-click on your script and choose “Edit.”
Your AHK script will already be pre-populated with some recommended entries. Leave them as they are. Press Enter two or three times to leave some space between them and your script.
Enter the following if statement that will create our basic rule.
#if WinActive("TYPE FILENAME") #if
The second “#if” marks the end of our if statement. The “TYPE FILENAME” is a placeholder for values we’ll see next.
Get Window IDs with Window Spy
Let’s see how to add a custom function to our script that will only be active on Make Tech Easier’s page.
1. Add the following to your script:
2. Save the changes and run your script by double-clicking on it. You’ll see a small message box appear. That’s how you create basic dialogs in AHK.
However, we’re using it because we need an AHK script active for easy access to AutoHotkey’s Window Spy. So, leave this messagebox active for now and turn your attention to AHK’s icon in the Windows tray.
3. Right-click on AHK’s little green icon and choose Window Spy from its menu.
4. Fire up your favorite browser and visit Make Tech Easier. Notice the top part of the information in Window Spy will show details about the active window. You need the “ahk_class,” “ahk_exe,” or “ahk_pid” to target a particular app. Let’s go with “ahk_exe” for our script. Since we’re using Firefox, Window Spy reports “ahk_exe firefox.exe.”
5. Copy both ahk_exe and the executable’s name in one sweep, then return to your script. Change it so that it reads:
#if WinActive("NAME") And WinActive("TYPE FILENAME")
Replace “NAME” with part of the active page’s title – in our case, we use “make” from Make Tech Easier. Replace “TYPE FILENAME” with what you copied before from Window Spy – in our case, “ahk_exe firefox.exe.”
m:: before “Msgbox, Done?” Your script should look like this:
#if WinActive("make") And WinActive("ahk_exe firefox.exe") m:: Msgbox, Done? return #if
Nothing will happen if you rerun your script and press m on your keyboard. However, visit Make Tech Easier with Firefox, press m again, and a familiar message box will pop up. You just created a window-specific shortcut!
Let’s turn the message box into something useful. Replace everything between the “ifs” to:
^b:: Send, I just copied %Clipboard% return
Rerun your script. Then, try to leave a comment under a post at Make Tech Easier. If you press Ctrl + B, you’ll see “I just copied X” appear in the reply box, where “X” will be the last thing you copied to the clipboard. You just remapped Ctrl + B to send the string “I just copied,” followed by the contents of the clipboard. Feel free to change it to anything you wish.
AutoHotkey uses the following symbols to create shortcuts with their equivalent keys:
- # for Windows
- ^ for Control
- ! for Alt
- + for Shift
You can also use these symbols with the Send command, which sends a text string to the active window. You can use that to remap existing shortcuts to different keys. For example, to remap Windows + B to work as “Ctrl + C,” you could change your script to:
#b:: Send, ^c return
It’s also possible to define multiple shortcuts. However, you can’t add two functions to the same key combination unless they’re in individual if statements that target different windows.
To create shortcuts for different apps and windows:
- Copy your script and rename it accordingly.
- Replace the “NAME” and “TYPE FILENAME” with ones that match another app or window.
- Enter your shortcuts, text expansion rules, and functions as we saw for the first script.
Now that you know how to create app shortcuts with AutoHotkey, find out how you can use AutoHotkey to automate almost anything in Windows.
Our latest tutorials delivered straight to your inbox