Matching Instant Hotkeys with a Loop (AutoHotkey Tip)

Sometimes You’ll Find a Loop a Simpler Way to Match Things

ProgrammingRobot

“…stuck in an infinite loop. Like I just said, it’s so depressing to get stuck in an infinite loop. Like I just said, it’s…”

If you’re new to AutoHotkey with little or no scripting experience, then this blog may venture too far into the weeds. I don’t like to put off new users because the journey into Windows scripting is well worthwhile. Most find it easy to get started with AutoHotkey with many simple-to-implement tools. However, it takes a little time to understand the nuances of the more advanced techniques. I recommend that AutoHotkey noobies start with the basics such as found in the “Introduction to AutoHotkey: A Review and Guide for Beginners” page. You’ll obtain immediate, rewarding results with basic AutoHotkey. Then, as your comfort with scripting increases, introduce yourself to more of AutoHotkey’s power with some of the slightly elevated topics.

*          *          *

In the last blog, by writing the IH_VarText(Var) function, I created a clever (even if I do say so myself) yet uneasy technique for linking the Instant Hotkey combination to the insertion text by converting the key combination (full of illegal variable characters) to a legal two-deep variable. While this worked, in most cases, it left a number of holes in the subroutine. Unless I added a trap line for every possible illegal key (e.g. the semicolon ” ; “ key, the slash ” / ” key, the hyphen ” ‘ ” key, etc), errors might occur. I needed to make a change.

Rather than directly linking the Hotkey combination to the insertion text, I decided to locate the proper insertion text after pressing the activating keys. I had saved the Hotkey combination in the two-deep variable created from the GUI name (e.g. Hotkey1, Hotkey2, Hotkey3, etc—also called a pseudo-array). I then saved the insertion text in the awkwardly formed two-deep variable via the new conversion function based upon the Hotkey combination (e.g. ^!o ⇒  ACo). I now alter course by generating a matching two-deep variable for the insertion text using the GUI number (e.g. IH_HotkeyText1, IH_HotkeyText2, IH_HotkeyText3, etc):

If KeyCombo !=
  IH_HotkeyText%GuiNum% := TextInsert
%A_Gui% := KeyCombo

This allows me to use the Loop command, first, to match the pressed Hotkey combination with the proper GUI number, then to recall the insertion text for that GUI:

TextAdd:

Loop, % IH_Count ; IH_Count equals the number of Instant Hotkey GUIs
 {
   If A_ThisHotkey = % InstantHotkey%A_Index%
    {
      SendInput, % IH_HotkeyText%A_Index%
      Break ; Stop looping
    } 
 }
Return

The variable IH_Count contains the number of Instant Hotkey GUIs created.

Note: In the InstantHotkeyLoop.ahk script, I changed the initial value of IH_Count from 1 to 0 and moved the incrementing code (IH_Count++) from the end to the beginning of the AddNewHotkey: subroutine. This shifted the normal value of IH_Count by 1 to match the total number of the Instant Hotkey GUIs rather than that count plus one.

As AutoHotkey Loops through the number of GUIs, the If conditional checks the built-in A_ThisHotkey variable (which contains the recently pressed Hotkey combination) against the incremental value of the pseudo-array InstantHotkey%A_Index%—which holds the Hotkey combination for each GUI. (The built-in AutoHotkey variable A_Index increments by 1 for each iteration of the Loop.) When AutoHotkey finds a match, the SendInput command inserts the corresponding text found in the IH_HotkeyText%A_Index% pseudo-array into any active document or editing field. After locating a match and inserting the text, the Break command terminates the Loop.

This approach to inserting text from the Hotkey combination allows us to eliminate the special variable creating IH_VarText(Var) function and its inadequacies. On the downside, the Loop forces us to search through all available GUI values to find a match. Next time, I use an associative array to bypass searching for the correct insertion text and directly access that text without delay. This provides a more elegant solution.

I discuss an example of associative arrays in “Chapter Seventeen: Reduce Code and Increase Power with the AutoHotkey Input Command” of my book Beginning AutoHotkey Hotstrings.

jack

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 )

Facebook photo

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

Connecting to %s