How to Create Application-Specific Keyboard Shortcuts with AutoHotkey

Isn’t it annoying that almost identical applications use different key combinations to achieve the same result? Worse still, don’t you hate it when software insists on using obscure keyboard shortcuts without offering the ability to remap them? Wouldn’t it be great to have absolute control over how your keyboard works in every app? Well, it is possible, thanks to AutoHotkey.

With AutoHotkey you can remap your entire keyboard or create custom hotkeys, both “globally” and for each app individually. If you want, you can even have custom text strings, or even entire templates, for each application bound to the same key combinations. All you need is AutoHotkey and a text editor – even Notepad will do. That looks interesting? So let’s jump inside.


Get started with Windows Spy in AutoHotKey

Many keyboards these days come with software to create custom shortcuts and macros. However, as we will see, AutoHotkey is more versatile because it is independent of the keyboard.

With it, your “keyboard customizations” will not be tied to a particular keyboard. You’ll be able to transfer your customization script to other computers and have your custom application shortcuts and shortcodes up and running in no time.

Since in this article we’re just getting started, if you want a proper introduction to AutoHotkey, check out our AutoHotkey quick guide for beginners.

Starts with download AutoHotkey from its official website. Then install it like any other app. You don’t have to run it afterwards. It automatically springs into action when you run a script designed for it. So, let’s create such a script.

USE VIDEO OF THE DAY

With AutoHotkey installed, right click anywhere, on your desktop or in a folder where you want to create your script. To choose New > AutoHotkey Script. Give a name to your future script and press Enter.


Windows 10 context menu option to create a new AHK script.

With AutoHotkey, you can create “global” shortcuts that will be active everywhere or application-specific shortcuts that will only work in an active application’s window. To “target” a specific application, you must identify it in your script. AutoHotkey can help you with its Window Spy.

Double-click your currently empty script to run it, and with it, AutoHotkey. Right-click the AutoHotkey icon in the Windows tray and choose window spy in the menu that appears.


Launched AutoHotkey window spy tool.

To find the credentials needed to target an application, leave Window Spy on your screen, and click on your application window. Then notice the ahk_class, ahk_exe, and ahk_pid entries at the top of the Window Spy window. In our case, we wanted to target the popular note-taking app Obsidian. Since other software might have similar ahk_class and ahk_pid, we used its executable file as the target, using ahk_exe Obsidian.exe, as mentioned in Window Spy.


Using AutoHotkey's Window Spy to find a window's ID.

When you know your target, it’s time to write a script.

It’s time to create a script in AutoHotKey

Right click on your script and choose Modify the scenario to open it in your default text editor. As you will see, it will be pre-populated with some values ​​which help with compatibility and performance. Ignore them, press Enter once or twice and target your application using:


#IfWinActive APP_IDENTIFIER

Replace APP_IDENTIFIER with the actual target you copied from AutoHotkey’s Window Spy. In our case, this resulted in:

#IfWinActive ahk_exe Obsidian.exe


AutoHotkey's IfWinActive indicates that everything below should only activate if a specific window is active.

When writing AutoHotkey scripts, you can use the following symbols for modifier keys on your keyboard:

  • ! for Alt
  • + for Shift
  • ^ for CTRL
  • # for windows key

Before creating your actual shortcuts, however, test whether the script will actually work only when the chosen application is active. The easiest way to do this is to use what AutoHotkey calls “a message box” or, rather, a “msgbox”.


Using AutoHotkey's MSGBOX command is an easy way to troubleshoot a script.

Type the following directly below the line where you targeted your chosen application:

^a::
msgbox it works!
return

If translated into plain English, it would look like:

  • When CTRL+A are pressed together on the keyboard…
  • … display a message box on the screen saying “it works!”.
  • When the user recognizes this message box, return to the previous state.

Run your script, press CTRL+A on your keyboard, and nothing should happen. That’s because you’ve targeted a specific app, but haven’t jumped to it yet. So, activate that app’s window, press the same combination, and you should see a message box pop up saying “it works”.

Now go back to any other app and try your key combination again. Hopefully nothing should happen. If so, that means your MSGBOX is only activating in your targeted application, which is the desired result we expect from this script.


AutoHotkey's MSGBOX should only show up when the defined keyboard combination is pressed in the targeted application.

If the hotkey “leaks” in other applications, double-check your syntax and make sure there is no typo in the selected target.

How to create custom keyboard profiles for your apps

AutoHotkey makes it easy to remapping what the keys on your keyboard do, both individually and when combined. Would you like to swap the A and B keys? In AutoHotkey syntax, it would look like this:


a::b
b::a

However, you probably don’t want to remap individual keys, but have multi-key combinations, with one or more modifier keys, performing specific actions.

Building on the previous example, if you want B to appear when you press CTRL+A and, vice versa, A to appear when you press CTRL+B, try:

^a::b
^b::a

Of course, this is just an example. In real life, pressing multiple keys to type a single character is the very definition of counterproductive. In contrast, assigning text strings to key combinations can significantly speed up text entry. To have your name, email address, or other text typed in when you press a key combination, you can use AutoHotkey’s “send” command. This “tells” the AutoHotkey, as the name suggests, to “send” the text string that follows it to the active window. In action, it might look like this:

^+O::
send Odysseas
return

Use AutoHotkey's Send command to enter entire strings of text into the targeted application.

In the script above:

  • We start by “telling” AutoHotkey to do something when we press Shift+CTRL+O at the same time.
  • This “something” sends the string “Odysseas”, which happens to be that writer’s name, to the active window.
  • Finally, with “return”, we state the equivalent of “that’s it, thanks, AutoHotkey!”.


The same key combination can enter your name in your note-taking app and use a health-replenishing potion in your favorite RPG.

Try experimenting with different key combinations and have AutoHotkey send various text strings to your chosen app. You can have multiple rules in the same script.

Using key combinations to enter text strings can be useful for instantly entering your name and email address. However, it is not intuitive when typing. After a while it becomes hard to keep track of what dozens of shortcuts are doing. This is where text expansion can help.

Instead of mapping specific key combinations to text strings, AutoHotkey lets you define shortcodes. Then, when it detects that you typed one, it can automatically replace it with a longer text string. It’s as simple as:

:*:MUO~::Make Use Of
  • The “:*:” at the beginning of the line indicates that this is a text expansion rule.
  • Next is the shortcode, which in our case is “MUO~”.
  • As with shortcuts, “::” is the logical equivalent of “=” in this scenario.
  • The last piece of the puzzle is the actual text string we want to replace “MUO~” with.

With this rule, every time we type UOM ~ in our targeted application, AHK will step in and replace it with Use.

Once you are done setting rules for one application, you can target another in exactly the same way. Use “#IfWinActive APP_IDENTIFIER” again, this time targeting another app’s window, and type your rules directly below.

Repeat as many times as you want, creating profiles of app-specific hotkeys and shortcodes.

Since AutoHotkey scripts are essentially text files, here’s a nifty idea: embed other scripts into yours and make them app-specific as well! Check out our list of cool AutoHotkey scripts. Pick the ones you like, but instead of using them as standalone scripts, open them in a text editor.

Copy their contents and add them to an app targeting section of your script. Save and rerun your script, and theoretically these scripts should work as part of yours when the application you targeted is active.

Make your keyboard smart with AutoHotKey

As you will find in the long run, creating such scripts is a process, not a one-time affair. As your demands and the way you use your software change, so will your scripts.


By continually expanding and tweaking your scripts, you may soon feel like hackers are represented in illiterate series. By pressing half a dozen keys and arcane combinations known only to you, you can magically conjure up a wall of text on your screen.


Keyboard entry
40+ Cool Productivity Keyboard Tricks Few Know About

Want to get things done faster? Here are over 40 powerful keyboard shortcuts that will save you the most time.

Read more


About the Author

About Raymond A. Bentley

Check Also

Finally, Apple’s iPad has a default weather app – TechCrunch

Hell has frozen over, apparently. Today, Apple addressed a long-standing user complaint about the iPad …