Make Your Hotkeys Easier by Combining a Rarely Pressed Key and a Simple Mouse Click…or Not! Plus, Take Advantage of More Little Used Keys in Hotkey Combinations
While the Hotkeys from two blogs ago for switching errant letters and the last blog for word swapping are simple enough—merely place the text cursor between the targets of the swap and hit the key combination—they can be made even easier my activating the Hotkeys with a click of the mouse. Rather than using two moves—placing the cursor, then hitting the Hotkeys—combine both into one action by making your left mouse button click part of the Hotkey.
This beginning Hotkey blog builds upon the discussions in the previous parts. If you find any of the information too confusing, then reviewing earlier blogs may be worthwhile.
New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”
* * *
In this blog we explore different approaches to making Hotkey combinations even easier to use. In AutoHotkey, there are a number of methods for simplifying Hotkeys. Each technique has its own advantages and disadvantages.
Take Advantage of Infrequently Used Keys with a Mouse Click
Duplicated on your Windows keyboard, the CTRL, ALT, SHIFT, and occasionally other keys appear on both the left and right side. This redundancy offers convenience when using the shortcut keys in particular areas of the keyboard. Since most people activate key combinations with one hand while holding the mouse in the other—depending upon whether your left or right handed—certain keys (left or right) tend to go unused.
For example, since I’m right handed, I tend to use my left hand for key combinations while holding the mouse in my right hand for reorienting the cursor and highlighting text. That leaves the right CTRL and ALT keys untouched—even when I’m not using the mouse. Take advantage of these lonely keys by adding them to a mouse click Hotkey—making your quick-and-dirty Hotkeys even quicker and dirtier.
Fortunately, AutoHotkey includes special key names for isolating those left and right keys. For example, the left and right CTRL and ALT keys are identified as LControl and RControl, and LAlt and RAlt, respectively. (See the AutoHotkey Keylist.) The left mouse button is identified by LButton. Combining one of these keys with the click of the left mouse button in a new Hotkey combination can immediately launch one of the scripts from the last two blogs.
Note: For the following snippets of code to work properly, the matching Hotkey routines from the previous two blogs must appear in the same script.
For swapping letters the right ALT key (RAlt) is combined with the left mouse button (LButton):
RAlt & LButton::
Send {Click}
GoSub, $!R ;label for letter swap Hotkey
Return
For swapping words:
RControl & LButton::
Send {Click}
GoSub, $!W ;label for word swap Hotkey
Return
Create a Hotkey combination with any two key names by inserting the ampersand (&) modifier between the two. (See this list of Hotkey modifying symbols.) The first key (which may be any key on the keyboard) becomes the modifying key for the combination and must be held down first before pressing the second key. In this case, the right ALT key is waiting for the left mouse click. If the click does not occur, then the LAlt returns to its native ALT function—which usually selects the top program menu bar.
Since the left mouse button is now part of the Hotkey combination, it does not pass on its native function—a left mouse click. Therefore, while clicking the left mouse with the left ALT key held down does activate the Hotkey, it does not reposition the text cursor as most mouse clicks normally would. I opted for the Send command to click (Send {Click}) at the mouse cursor hover location. (The mouse cursor must be hovering over the target location between the letters or words when the Hotkey combination executes.)
With the text cursor in place, the Hotkey routines from last two blogs ($!R for letter swapping and $!W for word swapping) are called with the GoSub command. Note that the GoSub Label names for the Hotkeys must be written exactly as they appear in the original Hotkey setup—including the $.
Rather than clicking first to locate the cursor, then activating the Hotkey, merely hover the left mouse at the appropriate spot while holding down either the left ALT key (for letter swap) or the left CTRL key (for word swap), then click the left mouse button. That gets the job done. (The word swap is more forgiving than the letter swap since clicking anywhere inside the first word or between the two words produces the same result. The letter swap mouse cursor accuracy must be right on.)
Using ~ to Send the Mouse Click
The tilde (~) modifying key may also be used to pass through the native function of a key—in this case the click of the left mouse button. However, I found that the click was so late for the letter swap that the called routine would not properly select the target letters:
RAlt & ~LButton::
GoSub, $!R
Return
However, the word swap script worked fine:
RControl & ~LButton::
GoSub, $!W
Return
I was able to fix the problem by adding a Sleep command delay to the letter swap routine:
RAlt & ~LButton::
Sleep, 200
GoSub, $!R
Return
Rather than guessing at an appropriate delay, I prefer my original positive solution which includes the Send {Click} command.
Using the Arrow Modifiers (< and >) for Left and Right
On the downside when building Hotkeys, the ampersand (&) modifier only allows two-key combinations. To add more than two keys to a combination, drop the & and add the left (<) or right (>) modifier arrows to indicate which side of the keyboard. For example, to create a three-key combination using the right CTRL key (>^), the right ALT key (>!), and the left mouse button (LButton) use the following for the word swap routine:
>^>!LButton::
Send {Click}
Gosub, $!W
Return
This tells AutoHotkey to use the right (>) CTRL (^) key, the right (>) ALT (!) key, and the left mouse button (LButton) simultaneously to activate the routine. This three-key combination can’t be done with the ampersand technique since it only supports two keys.
Other Keys as Modifiers
The major advantage to using the two-key ampersand (&) technique is that any other key can be used as the first modifier in a Hotkey combination. On most keyboards, there are a bunch of other rarely used keys: function keys; the number pad keys with NumLock on; the number pad keys with NumLock off, and other keys not on the list, but accessible with Scan Codes or Virtual Key numbers. (Scan Codes and Virtual Key numbers are a topic for next time.)
For example, The zero key (Numpad0) on the number pad can be converted into the modifier in combination with the left mouse button (LButton):
Numpad0 & LButton:: Send {Click} Gosub, $!R Return
However, there is one caution: the key’s native function will often be blocked—in this case inserting the number 0. Unlike the regular modifiers (CTRL, ALT, WIN, SHIFT) which do not block their native function, when most other keys are used in a Hotkey, their native function must be blocked. Otherwise, unwanted characters pop-up whenever using those Hotkeys.
We can take a look at the potential problem by adding the tilde (~) modifier to pass through the native function of the key. For example, by using the following unacceptable Hotkey setup:
~Numpad0 & LButton::
pressing the Numpad0 key first immediately starts inserting zeros. By default, this native output must be blocked. But, how do we make the key work properly when pressed on its own? A new Hotkey must be added which returns its original function:
Numpad0::Send 0
Now rather than immediately sending zeros, the character is inserted when the Numpad0 key is released (without any other key in combination). What gets lost in this setup is the autorepeat caused when holding down the key.
The Mouseless Hotkey
The Hotkey can be shortened to just one key press by dropping the mouse click from the combination:
Numpad0:: Send {Click} Gosub, $!R Return
I don’t know how much this one-key approach helps in this situation since, in any case, the mouse cursor must hover over the proper location. If not, the action occurs wherever the mouse cursor happens to hover. But if you have no other use for particular keys, then there is no quicker way to activate a Hotkey routine then pressing just one key.
Next time, we’ll look at using Scan Codes and Virtual Keys. What are they and how do we use them? Make better use of alternative keys not identified in the AutoHotkey documentation.