Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Remap and Set Global Hotkeys on Windows 10 with Auto Hotkey

blog/cards/remap-and-set-global-hotkeys-on-windows-10-with-auto-hotkey.jpg

Remapping and overriding global hotkeys can be tricky, but here's an easy way to do it with a tool called AHK.

Quick Jump: Downloading Auto Hotkey (AHK) | Creating the AHK Script | Running the AHK Script

Windows 10 comes with over 50 global hotkey combinations assigned to the Windows key, such as being able to press Win + e to open Windows file explorer, which is quite handy but personally I find most of the preassigned hotkeys to be not that useful.

A global hotkey is a hotkey that works across your system, and isn’t tied into a specific application. That means if you press it from anywhere, that action will trigger and in this article we’re going to go over how to set our own custom global hotkeys and even be able to override the ones that come with Windows without having to reboot or touch the Registry.

Downloading Auto Hotkey (AHK)

The tool we’re going to use is called Auto Hotkey (AHK) which is an open source tool for Windows that lets you automate all sorts of things, but remapping keys and setting global hotkeys is something it does very well.

You can download it from their downloads page. I recommend grabbing the latest stable release which is v1.1.x at the time of writing this article. Then install it after you download it.

One nice thing to note is unlike most “modern” applications running the AHK script that we’re going to create in this article only uses 2.5mb of RAM and doesn’t use any idle CPU resources at all.

Creating the AHK Script

AHK is driven by a scripting language and doesn’t require doing anything too crazy to get it up and running. You just create your script and then run the AHK file.

So, create a new file called hotkeys.ahk anywhere you want on your computer. You can also choose to name it something else as long as it ends with .ahk.

Remapping Keys and Setting Global Hotkeys

Lines that start with ; are considered comments and are ignored by AHK.

; Remap hotkeys
CapsLock::Esc
Esc::CapsLock

; Global hotkeys
#c::Run, explorer.exe D:\courses\deploying
#i::Run, C:\Program Files\GIMP 2\bin\gimp-2.10.exe
#n::Run, explorer.exe D:\src\sites\nickjanetakis
#m::Run, foobar2000.exe
#t::Run, %UserProfile%\AppData\Local\wsltty\bin\mintty.exe --WSL="Ubuntu-18.04" --configdir="%UserProfile%\AppData\Roaming\wsltty" -~
#+t::Run, *RunAs %UserProfile%\AppData\Local\wsltty\bin\mintty.exe --WSL="Ubuntu-18.04" --configdir="%UserProfile%\AppData\Roaming\wsltty" -~
#w::Run chrome.exe
#+w::Run chrome.exe -incognito

There’s a number of things that are specific to my set up, so let’s break it down because chances are you’re going to end up modifying most of those global hotkeys to work for you.

Remapping keys:

Since I started using Vim about a month ago, one thing I’ve done is remap my escape key with caps lock. I really like this change, and it only took about a day to get used to it.

To remap keys with AHK, you can add lines such as: KEY_OLD::KEY_NEW. You can view a full list of keys on the official AHK site.

It’s worth mentioning that you need to bind both sides of the remap. In this case, we’re remapping caps lock to escape but we also need to remap escape to caps lock.

The end effect is both keys will be swapped as if you switched them on your keyboard. Also, if your caps lock key has a light, pressing escape will toggle it so you won’t lose that feature.

Setting global hotkeys:

Let’s break down the first line: #c::Run, explorer.exe D:\courses\deploying.

The overall pattern is:

  • # Modifier keys(s) (here’s a full list)
  • c The key you want to bind it to
  • :: Setting up a remapping (part of AHK’s API)
  • Run The thing you want to do (part of AHK’s API)
  • explorer.exe The program you want to run
  • D:\courses\deploying Optionally set the program’s arguments

You could think of this as we’re binding Win + c to run the Windows file explorer program and open up a specific directory.

Combining modifiers:

We can see an example of that in the last line with #+w::Run chrome.exe -incognito.

I have Win + w set to open up a regular Chrome window, but Win + Shift + w opens up Chrome with incognito mode. That’s handy for when I want to record courses, since I don’t want my personal settings coming up on video.

Launching programs as an administrator:

You’ll notice that I have 2 hotkeys to launch a terminal window. The second one includes #+t::Run, *RunAs. The *RunAs allows you to run a program as admin.

Occasionally I still use Vagrant and running Vagrant on Windows requires running it as admin and since I interact with vagrant.exe from inside of WSL, it requires launching a terminal as admin which is why I created this shortcut.

Changing the hotkeys to work for you:

I’m launching a few directory paths and programs that you might not have, but I included them here so you can see what type of things I’m launching with global hotkeys.

You should replace them with what works for you.

But here’s 2 tips that you might find handy when it comes to adding hotkeys.

  1. I like keeping my key binds in alphabetical order as it makes it easier to scan.

  2. I like using keys to describe the type of program I’m launching, not its name. For example, I use “i” to open an image editor, which happens to be GIMP.

    In another example, if I switch to Fire Fox, my key bind will still be Win + w to open a web browser instead of having to change it around to match the program’s name.

It’s also worth pointing out I prefer using explorer.exe instead of Explorer because it’s explicit that I’m launching an app. Plus, with the .exe style, you can pass in arguments (such as what I’m doing with Chrome). That wouldn’t have worked if I used Chrome -incognito.

AHK syntax highlighting in your favorite code editor:

Chances are your code editor has support for AHK syntax highlighting, with or without a plugin. I recommend checking that out since AHK is a legit scripting language.

blog/autohotkey-syntax-highlight-vim.jpg

Vim supports Auto Hotkey by default.

Running the AHK Script

Now that we have the script ready to go, the only thing left to do is run it.

Chances are when you installed AHK, it registered the ahk file extension. If you see an “H” icon for your file, then you’re good to go. All you have to do is double click it to run it.

If you don’t see that, then right click your file and go to “Open with” and then choose Auto Hotkey from the list of applications to use.

After running it, it will appear in your system tray (by your clock). At this point you can try out your hotkeys. They should work as long as you don’t have a syntax error in the script.

Reloading an AHK Script after Editing It

If you decide to edit your script to add more hotkeys, you can right click the script in your system tray and choose “Reload script” from the menu. There’s no need to restart it.

Other interesting AHK features:

Auto hotkey is much more than a hotkey script which is why it has other useful tools such as the Window Spy which lets you find out a bunch of low level details about any window.

This Window Spy is a right click menu option of your script.

For example if you were wondering what the exact executable name is for an already open program, you can run the Window Spy, enable “Follow Mouse” in the top right and then mouse over the program you want to learn more about.

It will show the ahk_exe line in the first text box up top. That would be the name of the program you would run in your hotkey script.

Running Your AHK Script on Windows Startup

Your hotkey script will only work if it’s running, and by default AHK scripts don’t start up when Windows boots up, so we’ll have to do that ourselves.

  1. Find your hotkeys.ahk file in Windows explorer and copy the file itself
  2. Hit Win + r to run the Windows launcher (if you haven’t already overwrote that key :D)
  3. Run shell:startup
  4. Right click anywhere and choose Paste shortcut to paste it into the Startup folder
  5. Rename the file to hotkeys.ahk since the - Shortcut part isn’t useful

If you did it correctly the icon should have a little arrow in the bottom left. This is a shortcut (comparable to a symlink) to your real script.

The Start Menu > Programs > Startup path is a special path where anything here will automatically startup when Windows boots up.

If you ever want your AHK script to no longer startup with Windows you can delete the shortcut here. It will not delete the original script file.

And that’s all there is to it for working with global hotkeys in Windows.

What hotkeys are you going to set up with AHK? Let me know below!

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per month (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments