AutoHotkey Tips: Multiple Hotkeys and Using The Extra Mouse Buttons

Beginning Tips for Assigning Duplicate Hotkeys for the Same Operation and Issues with Using XButtons on Your Mouse

I recently completed a series of blogs on the ins and outs of AutoHotkey Hotstrings. I was amazed at how much more there was to the topic than I expected. (It was enough to turn it into another e-book.) This prompted me to start taking a closer look at AutoHotkey Hotkeys—the primary impetus for AutoHotkey’s split from the original AutoIt. This blog is the first of many on the topic of Hotkeys.

I don’t know if I’ll write enough blogs for another book. This series may wrap up after a few useful articles. However, it is a beginning topic worth pursuing. The more that’s known about how AutoHotkey works, the easier it is to write useful scripts. This time I offer a Hotkey technique I use with my QuickLinks menu script and a solution to a mouse XButton problem.

The QuickLinks Script

I’ve written many scripts over the years merely for demonstration purposes. (Hopefully, those demos help spark more ideas for other scripts.) But, because it’s so easy to use, I always load the QuickLinks menu app as part of my default AutoHotkey script. I find QuickLinks easier than using the Start button, doing a search, or crowding too many icons onto the Windows Desktop or Taskbar. The QuickLinks menu pops up with a click of the first extra mouse button (XButton1)

QuickLInks
The QuickLinks AutoHotkey script creates a tailored menu of favorites, making folders, Web sites, and programs quickly accessible with a click of the first extra mouse button (XButton1) or Hotkey combinations (left and right hand).

Originally, I set up the script to open the QuickLinks menu using the Hotkey combination WIN+Z (#z):

#z::Menu, QuickLinks, Show

Since the menu structure is preloaded in the auto-execute portion of my script, I only needed one command to Show the Menu.

That works great if you’re right handed (mouse in right hand and keyboard with left), but it is awkward for anyone who uses the mouse with their left hand. So, I added another easy Hotkey combination for the right hand, ALT+Comma (!,). I did this with an almost identical Hotkey statement:

!,::Menu, QuickLinks, Show

This also works, but, if your Hotkey command is more than one line and includes a longer subroutine, the entire snippet of code needs to be repeated. Depending upon the number of lines, there could be quite a bit of redundancy.

There are a number of solutions for this multi-Hotkey problem. First, I’ll give a few alternative solutions, then I’ll explain the easy way to create multiple Hotkeys.

The Hotkey as a Label (Subroutine)

One way to create an additional Hotkey for any script is to call the previous Hotkey combination as a subroutine. Whenever a Hotkey combination is created, it automatically creates a Label by the same name. (A Label is a named subroutine.) For example, the line:

!,::GoSub #z

would have the same effect as the previous Hotkey by running the command found in the WIN+Z Hotkey definition. By using the GoSub command, the new Hotkey (!,) calls the old Hotkey (#z) as a subroutine.

Hotkey Runs a Function

Alternatively, multiple Hotkeys for the same routine can be created by turning the command lines into a function. Any snippet of code can be turned into a function, then run by any Hotkey combination. As an example:

!.::HotFunction()

HotFunction()
{
  MsgBox Hello!
}
!/::GoSub !.
!,::HotFunction()

In this case the MsgBox command is placed inside the function HotFunction(). Anytime the function is called, a window pops up displaying the word  “Hello!” The first Hotkey, ALT+DOT (!.), directly calls the function. The second Hotkey, ALT+Forward Slash (!/), calls the first Hotkey (!.). The last Hotkey, ALT+Comma (!,) also directly calls the function. All three Hotkey combinations work.

Note: I’ve placed the Hotkeys in the above example in an almost random order to illustrate that they can appear anywhere in a script (except inside the auto-execute section) since each Hotkey and the function HotFunction() are standalone modules. The order does not matter as long as each does not interfere with another subroutine.

The Hotkey Command

There is yet another way to recreate multiple Hotkeys. Unlike Hotstrings which tend to be permanent once they are loaded, there is a command available to manipulate and turn Hotkeys on and off. It is cleverly called the Hotkey command. The Hotkey command allows us to add flexibility to our implementation of HotKeys. A redundant Hotkey combination for the example above can be set up on the fly with the line of code:

Hotkey, !',!/

The new Hotkey combination is ALT+Hyphen (!’) and runs the Label subroutine named ALT+Forward Slash (!/)—which is a previously defined Hotkey.

It important to note that this Hotkey command must appear as a command either inside a subroutine or in the auto-execute section of the script which runs when the script first loads. Otherwise, AutoHotkey will ignore it. For example, in the script above, the line of code (Hotkey, !’,!/) must appear as the first line in the script—before any of the regular Hotkey definitions.

(More space will be devoted to the Hotkey command in future blogs.)

Multiple Hotkeys the Easy Way

As shown above, there are plenty of ways to create multiple Hotkeys without using too much redundant code, but AutoHotkey has a much simpler built-in method. As long as any number of Hotkeys with no action command are listed sequentially in a column (no extra lines between each), they all take on the same function as the last Hotkey in the list. For example, the three Hotkeys above can be reduced to:

!.::
!/::
!,::MsgBox Hello!

In other words, “multiple hotkeys can be stacked vertically to have them perform the same action.” In the QuickLinks example, the following adds the left and right Hotkeys, plus the click of the first extra mouse button (XButton1):

#z::
!,::
XButton1::Menu, QuickLinks, Show

All three should do the same thing. Now the QuickLinks menu pops up whenever any of these actions are taken.

Tip: If you have an AutoHotkey app which you want immediately available with the click of an extra button on your mouse, then XButton1 (fourth button) or XButton2 (fifth button) may do the job for you.

There’s one possible problem. If you’re using a Logitech mouse with the SetPoint software, the XButton1 Hotkey may not work—although the other Hotkeys should continue operating. (This same problem may exist for other types of input devices.)

Fixing the XButton1 Mouse Problem

While AutoHotkey Hotkey combinations normally take precedence over other program and Windows Hotkeys, this is not the case with the Logitech SetPoint mouse software. The extra mouse button is not overridden by AutoHotkey and continues with its default action.

Although there may be an AutoHotkey fix, in this case, it’s probably much simpler to reset the action of the mouse button in the SetPoint software. Once you know which Hotkey does the job, it’s merely a matter of opening the SetPoint software (to open SetPoint either search for SetPoint or locate it through the Mouse Properties window found in Mouse in the Control Panel) and assign the Hotkey combination to the appropriate button.

Logitech SetPoint
Select the button (5) and select the task (Keyboard Assignment), then enter the Keystrokes into the appropriate field. Only CTRL, ALT, and CTRL+ALT work as activating keys in SetPoint.

I used the ALT+Comma combination since SetPoint does not accept the WIN key. Now the first extra button always runs the assigned Hotkey—my QuickLinks menu.

While using XButton1 is a more generic way to use extra mouse buttons, it doesn’t always work. There are just too many different input devices to account for all the possibilities in AutoHotkey. Plus, who knows how hardware will change in the future.

Using the utility software which comes with an input device is an excellent way to add many of your most useful scripts to a click of your mouse while taking advantage of extra buttons. In addition, AutoHotkey only directly supports a fourth and fifth mouse button (XButton1 and XButton2) respectively. SetPoint (or other mouse utility software) gives you control over even more buttons—if they’re available.

Another advantage to this approach is that you won’t need to rewrite your AutoHotkey script for each Windows computer—although set up through SetPoint type software will be needed for each computer.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s