Turn Any Action into a Keyboard Shortcut


The free, open source scripting language AutoHotkey may not be one of the most powerful or popular programming languages on the planet, but that's okay—it's not just made for programmers. That's because AutoHotkey is well within the grasp of regular folks like you or me—people who have a fair understanding of computers and are willing to learn just a little to make major strides in productivity. Today I'll show you how to use AutoHotkey to turn almost any action into a keyboard shortcut. NOTE: I'm not a computer programmer by trade. In fact, I graduated from college a few years ago with a degree in Philosophy of Religion, Arts, and Science (a liberal arts degree I made up). My point is, even if you have absolutely no programming experience, creating simple keyboard shortcuts with AutoHotkey is well within your grasp.

Before You Get Started

new-script.pngFirst thing's first: You're an amateur programmer now, so you need to go download and install AutoHotkey. Once you've got that done, open a folder, right-click, and select New -> AutoHotkey Script. Give it whatever name you like, then open it up with your favourite text editor (I recommend Notepad++).

You can also grab a script of all the examples I discuss below here if you'd like to use it as a starting point.

What is a Hotkey?

In AutoHotkey, you can create keyboard shortcuts or remap keys easily in more than one way, but today we're going to focus on one method: Hotkey labels. The syntax of creating a hotkey is very simple, and can be used in two ways.

First, if you want to do something very simple—like remap a key—it looks like this:

hotkey::remapped key

...where hotkey is the keyboard shortcut that will activate the second part—in this case, a remapped key. That may seem rather vague, so let's look at a concrete example. I don't like the Capslock key as is, preferring instead to remap it to my Control key. With AutoHotkey, all it takes is:


If you add that small snippet of code to the AHK file you created above and then run the file (just double-click it), you'll notice that your Capslock key now works as a control key instead. Now not only have you got your control key at a much closer, less stressful range for your pinky, but you're not likely to accidentally fire the Capslock key when you don't want it. However, if you don't want to lose the Capslock key altogether—as there are times it can come in handy—you can add the following to your AutoHotkey script (Thanks mc_spanky_mcgee):


With this hotkey, the plus sign (+) stands for Shift, so hitting Shift+Capslock will turn on and off the Capslock key so that turning on Capslock requires a much more deliberate process. For a full list of modifiers you can use to create hotkeys, check out this page. For a better idea of which symbols you can use, from Capslock to Tab to the Spacebar, check out the full AutoHotkey key list.

So far so good, right? You can actually remap almost any key in this way—including regular, non-modifier keys. So if you wanted to turn your "k" key into an "i", it'd be as simple as:


Not that remapping k to i would be terribly useful, but you get the idea. It is terribly simple.

Taking Hotkeys a Step Further

Now that you've got an idea of how to create hotkeys the simple way, we'll move on to slightly more advanced hotkey creation. First, we'll create a simple hotkey that will open Lifehacker when we press Windows-l (who wouldn't rather read Lifehacker than lock their desktop?). Quite simply, it looks like this:

#l::Run, http://lifehacker.com/

In this example, we're using the Run command, which can take any target—from web URLs to files on your hard drive—and, quite simply, open them.

As a result, creating a keyboard shortcut to launch anything at all is a breeze. You can launch any program, document, or web page with a simple shortcut of your choosing. If you were creating an iTunes shortcut with Windows-i (where the Windows key equals the pound sign [#] ), for example, it might look something like this:


You'll noticed I introduced another concept here: variables. The variable %A_ProgramFiles% tells AutoHotkey to look in my default Program Files directory—in my case, "C:\Program Files". I could have just made the command Run, C:\Program Files\iTunes\iTunes.exe, but using the variable means that—assuming I've got iTunes installed—the same shortcut will work on other computers that have iTunes installed to the default directory, even if their home drive is D:\ or F:\. For more on variables, check out AutoHotkey's introduction to variables, along with their list of built-in variables (like %A_ProgramFiles%).

Creating More Complex Hotkeys

So far our hotkeys have been very simple, one-line affairs, but sometimes you need more than that. In those instances, you can create multi-line actions that you want to occur when your hotkey is triggered. This requires a slightly different syntax.

hotkey:: Do one thing Do more things... return

Basically, as you can see, it starts out the same way with the hotkey followed by two colons. Then, however, you break to a new line and write you first action, followed by however many you want, and it ends with "return" (which signifies that the hotkey is done executing). So let's put it into practice.

The following keyboard shortcut, Windows-t, will automatically empty the Recycle Bin when I press it. When it's finished, it will show me a message telling me that the trash has been taken out.

#t:: FileRecycleEmpty, C:\ MsgBox, The trash has been taken out. return

empty-trash.pngIn the hotkey created above, I used AutoHotkey's FileRecycleEmpty command, which takes the drive letter where the bin is located as a parameter. I also used another new concept: the MsgBox command, which displays the text after the command in a window. As you can see, I used it to confirm that the command was run and the trash was taken out.

Restrict Your Hotkey to a Specific Application

Sometimes you want to create a hotkey that will only be applicable to one specific application. In those cases, you need to use the #IfWinActive directive. To use it, you need to place #IfWinActive WindowType (where WindowType is the window or app you want the shortcut to apply to) followed by the hotkey, then followed again by #IfWinActive without any WindowType (so that all following hotkeys won't be restricted to one window or application). In the example below, I've set the Windows-o hotkey to open the Options in Firefox.

#IfWinActive ahk_class MozillaUIWindowClass #o:: Send {Alt}t Sleep 100 Send o return #IfWinActive

autoit3-window-spy.pngSo let's dive in and examine this bit of code. First, you'll notice the ahk_class MozillaUIWindowClass bit. That may seem intimidating, but all it does is tell AutoHotkey that this shortcut will only work when a program using the MozillaUIWindowClass (like Firefox or Thunderbird) is active. You can grab the ahk_class using the AutoIt3 Window Spy, which you'll find in your AutoHotkey install directory. Just run it and click on the window you want to restrict a hotkey to to grab the window class and that's a good starting point.

Next, we've used the Send command, which sends literal keystrokes to your window. The first one I sent was Send, {Alt}t, meaning that the bracketed text, Alt, indicates a modifier (again, go to the Send page for a closer look at modifiers)). If you were to press Alt-t in Firefox right now, you'll notice that the Tools menu drops down.

Then I sent the command Sleep 100, which tells the script to wait 100 milliseconds before going to the next command. I do this just to make sure the computer has time to react to my first command and the Tools menu is open. Then I sent the "o" key to select Options from the Tools drop-down menu. Finally, I ended the hotkey with the return followed by #IfWinActive to ensure any other hotkeys beyond this one aren't limited just to Firefox or Thunderbird (unless that's what you wanted).

Take Your Tweaks with You

The great thing about AutoHotkey is that you can compile your scripts to portable executables that can run anywhere by simply right-clicking the file and selecting Compile. Drop the resultant EXE on your thumb drive and take it with you wherever you go.

And Then Some

Actually, despite the fact that AutoHotkey is a very simple-to-learn language, it can be much more robust and powerful if that's what you're looking for. For example, lots of Lifehacker Code projects I've written—like Texter, Swept Away, Rocker, and ClickWhen—were all created using AutoHotkey. So while you can do tons of very simple programming with AutoHotkey like creating keyboard shortcuts, you can do a lot more if you're interested. Windows automation and text replacement are also AutoHotkey fortés.

If you are interested in learning more about AutoHotkey, I'd highly recommend bookmarking the AutoHotkey commands page and visiting their forums to read up on AutoHotkey and ask questions. They're a very helpful bunch. (Thanks engunneer!)

If you're an AHK fan, let's hear what good uses you put it to in the comments.

Adam Pash is a senior editor for Lifehacker who loves some automation and keyboard shortcuts. His special feature Hack Attack appears every Wednesday on Lifehacker AU.


    Cool. Finially found a program that could do this form me ! I was sick of always holding control or alt down all day, so now have 1 key options for these commands.

    ; Assign Keys on keyboard for quicker computer use.
    ; ` = Toggle between applications (Alt-Tab)
    ; F1 = Cycle Through ALL running applications (Alt-Shift-Tab)
    ; F2 = Reserved (Rename)
    ; F3 = Chrome Window Left (Control-Shift-Tab)
    ; F4 = Chrome Window Right (Control-Tab)
    ; F5 = Refresh (Unchanged)
    ; F6 = Cut (Control-X)
    ; F7 = Copy (Control-C)
    ; F8 = Paste (Control-V)
    ; F9 =
    ; F10=
    ; F11= ? (Opera/Chrome Fullscreen)
    ; F12=

    SC029::Send {Alt down}{tab}{Alt up}
    F1::Send {Alt down}{LShift down}{tab}{LShift up}{Alt up}
    F3::Send {Ctrl down}{LShift down}{tab}{LShift up}{Ctrl up}
    F4::Send {Ctrl down}{tab}{Ctrl up}
    F6::Send {Ctrl down}{x}{Ctrl up}
    F7::Send {Ctrl down}{c}{Ctrl up}
    F8::Send {Ctrl down}{v}{Ctrl up}

Join the discussion!

Trending Stories Right Now