Wednesday, March 12, 2008

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.

Do one thing
Do more things...

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 your 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.

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

In 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
Send {Alt}t
Sleep 100
Send o

So 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 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).


Post a Comment

Related Articles

Related Article Widget by Hoctro

About Me


Hi, I am Hua, a chinese expat residing in India excited about windows, linux and all things tech

Receive updates for free

Enter your email address:

Delivered by FeedBurner

Mobile Version


Bookmark and Share

Blog Archive