Create Instant Temporary AutoHotkey Hotstrings with the Input Command, (Part 12, Beginning Hotstrings)

For Those Times When All You Need for Special Tasks Is Temporary Hotstrings, Use the AutoHotkey Input Command

*          *          *

New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”

*          *          *

Beginning AutoHotkey Hotstrings 200pxAs a convenience for people who want to get this entire series (plus more) in one place, I have now published the e-book Beginning AutoHotkey Hotstrings which is available on the ComputorEdge E-Books site (in three formats—EPUB for mobile devices, MOBI for Kindle, and PDF for printing and computer) and through Amazon. Regardless of whether you’re interested in the book or not, it’s worth your time to peruse some of the blogs linked at “Beginning AutoHotkey Hotstring Techniques” found under the “AutoHotkey Topics and Series” tab in the top menu bar. They just might inspire your next AutoHotkey script.

*          *          *

A downside to AutoHotkey Hotstrings is that once loaded they are always in force (unless you use the #If or #IfWinActive command to create context sensitive Hotstrings—thus isolating the action to particular programs). You may want the freedom to use specialized Hotstrings in any Windows program or Web editing field, but prefer them to remain inactive most of the time. That’s where the Input command is helpful.

The AutoHotkey “Input” Command

ComputorEdge AutoHotkey E-BooksWhile most of the time you will want to use the simpler built-in Hotstring format in your AutoHotkey AutoCorrect scripts, there are situations where it’s better to use the Input command to create temporary hotstrings. Use AutoHotkey Input:

  1. When the Hotstring activating text is easily confused with other common text entries. For example, using the two-letter string “me” for expansion to the state name “Maine” in standard Hotstring format would activate the replacement anytime you use the common self-referential pronoun “me” in any context. The same is true for the month abbreviation “Jan” (a person’s name)…or the weekdays “Wed”, “Sat”, and “Sun.” All of them are also standalone words (or names)—making them unsuitable as full-time activation strings.
  2. When the text expansion is only needed in specialized situations and not for general AutoCorrection. For example, a list of acronyms which expand to their full names may only be needed when working on documents which contain those acronyms. When using the Input command for Hotstring replacement, the action is often activated with a Hotkey combination. Once the text expansion (or other action) executes, the Input command is no longer active.

The online AutoHotkey documentation for the Input command includes an excellent example of how it can be used. I’ve modified that example to create temporary Hotstrings for all 50 states plus the District of Columbia by typing the two-letter postal code. (The complete StateInsert.ahk script can be downloaded from the ComputorEdge AutoHotkey download site.) The following is an excerpt from the complete script:

Input, UserInput, V T10 L2, {enter}.{esc}{tab}
      , al,ak,az,ar,ca,co

if (ErrorLevel = "Timeout")
  MsgBox, You entered "%UserInput%" input timed out.
if (ErrorLevel != "Match")
  Send, {backspace 2}
  MsgBox, "%UserInput%" is not a state abbreviation.
if (ErrorLevel = "NewInput")
if InStr(ErrorLevel, "EndKey:")
  MsgBox, Input terminated with %ErrorLevel%.
; Otherwise, a match was found.
if (UserInput = "al")
  Send, {backspace 2}Alabama
else if (UserInput = "ak")
  Send, {backspace 2}Alaska
else if (UserInput = "az")
  Send, {backspace 2}Arizona
else if (UserInput = "ar")
  Send, {backspace 2}Arkansas
else if (UserInput = "ca")
  Send, {backspace 2}California
else if (UserInput = "co")
  Send, {backspace 2}Colorado

(The above snippet of code only works for the first six states. The script StateInsert.ahk includes all 50 states plus Washington, D.C.)

The first line of code (!]::) creates the Hotkey combination Alt+] (the ALT key (!) plus the close square bracket (]) pressed simultaneously. Anytime the ALT key is held down while pressing the ] key, the Input command is activated.

(In the online example, only the open square bracket key [ is used as the Hotkey. This is probably not a good idea since anytime that key is hit (accidentally or otherwise), the script will activate—most likely causing confusion. Adding the ALT key (or another Hotkey modifier) makes mistakes far less likely.

How the “Input” Command Works

The Input command offers much greater flexibility than the standard Hotstring double-colon setup (::fl::Florida), but along with the possibilities comes a little more complexity.

The first thing to remember is that unlike standard Hotstrings which are always in force while the script is running, the Input command is active for one cycle only. Once the Input command completes its action, it lies dormant until the next activation via the Hotkey.

Second, once initiated, the Input command captures all keyboard activity until it either completes its mission, times out, or is exited via an EndKey (e.g. ESC, ENTER, TAB).

In the following line of code:

Input, UserInput, V T10 L2, {enter}.{esc}{tab}
     , al,ak,az,ar,ca,co

the variable UserInput stores the value of the keys pressed during activation. UserInput saves the keystrokes for activating the planned text expansion—in this case the two-letter state code is replaced with the state name.

(The above line of code has been wrapped for display purposes using AutoHotkey line continuation techniques.)

Following the second comma is a series of options separated with spaces (V T10 L2). The first option is V for Visible. By default, keyboard activity during a running Input command is not displayed on the screen. This is useful for activating non-text replacement activities. However, I found that when I removed the V option from the above example, the results were not as I expected. Cursor movement needed adjustment. It was easier to leave the visible (V) option in place.

The T option designates the amount of time in seconds (T10) AutoHotkey waits before the Input command terminates. If a T value is not included, the script will pause indefinitely. Unless you want to demand some type of user action, it is probably best to include the Timeout option T.

The last option in this example is L for the max length of the input string. Since all of the state abbreviations are two characters long, L2 is added. With this L value the instant two keys are pressed, the Input command completes its work—even if it’s not a matching abbreviation. Without the included L option, Input would continue to wait when a wrong abbreviation is entered until either the timeout or an EndKey is pressed. However, any true match would instantly continue to the result.

The next set of parameters (following the third comma) are the designated EndKeys. When pressed, any of these ({enter}.{esc}{tab}) will terminate the Input statement. It is important not to place a space between these values unless you want one of the terminating keys to be the SPACEBAR.

Finally, the last set of parameters is a list of all the text values which represent a true value for UserInput (al,ak,az,ar,ca,co). The list is separated by commas and should not include spaces unless they are part of a true value. As keys are pressed during the active Input command, they are checked against this list. If a string is found, the script instantly moves on to the next step and the Input is terminated.

No True Value Found

When the Input command is terminated or the action completed, the variable ErrorLevel is set. There are a series of If conditionals which display an AutoHotkey MsgBox explaining why the Input failed. The one conditional I added was for those times when a wrong state abbreviation is accidentally entered. Afterall, it’s not easy to remember all those abbreviations:

if (ErrorLevel != "Match")
  Send, {backspace 2}
  MsgBox, "%UserInput%" is not a state abbreviation.

Since the length option is set to two (L2), any two-letter combination which does not match an item in the list will instantly generate an ErrorLevel value of not Match (if (ErrorLevel != “Match”)). The Send command is used to delete the mistyped characters from the screen (Send, {backspace 2}) and the MsgBox is opened informing the user of the error.

State Abbreviation Text Replacement

The final section of the script is a series of identical conditional statements—one for each state. The Else If construction is used to force AutoHotkey to stop checking Else If statements as soon as the first matching conditional is found.

The Send command backspaces to remove the entered state id text, then inserts the state name (Send, {backspace 2}Alabama).

A Quirk of the Input Command

The Input command suspends other AutoHotkey activity without resetting other standard Hotstring activity. In other words, once the Input is completed or terminated, things take up where they left off. For example, when I entered ne for Nebraska, after hitting the SPACEBAR, I got Nebrasné.

As it turns out, I have an AutoCorrect Hotstring in my standard script which converts the letters ne to —for “born as.” When I activated and used the StateInsert.ahk script, even though entering ne initially returned Nebraska, hitting the SPACEBAR changed it to Nebrasné. The keyed in characters ne continued active as the Hotstring for —in spite of the fact that the text Nebraska had been inserted by Input. To leave Nebraska unchanged the standard Hotstring needed resetting.

One of the methods for resetting Hotstring activity is clicking the left mouse button. Adding a Click at the text insertion point coordinates (A_CaretX, A_CaretY), causes the active Hotstring to reset. However, since the inserted Input text is ignored by the text cursor Click command, it is necessary to move the cursor 6 spaces to the right:

Send, {backspace 2}Nebraska{click
       , %A_CaretX%, %A_CaretY%}{Right 6}

(This line of code has been wrapped for display purposes using AutoHotkey line continuation techniques.)

This may seem a little complicated and excessive for a state name I rarely ever use. Another option is to remove the ne Hotstring conversion, which I almost never need, from my standard AutoCorrect script, thus eliminating the conflict. (The above code can be found in the StateInsert.ahk script and is available for download in the ZIP file StateInsert.ahk from the ComputorEdge AutoHotkey download site. For information about other available AutoHotkey scripts, see Free AutoHotkey Scripts and Apps for Learning Script Writing and Generating Ideas.)

Other Uses of the Input Command

After playing with the Input command, it’s easy to see that it could be used for a variety of other activities. Virtually any snippet of AutoHotkey code can be included in an Input statement. One possibility is adding an unlimited number of temporary Hotkeys for any purpose without making them permanent with the standard Hotkey syntax (which could interfere with other system hotkeys). By using the Input command, one Hotkey combination can activate a series of other Hotkeys by merely pressing one additional key.

*         *          *

Find Jack’s AutoHotkey e-Books at




Leave a Reply

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

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

Facebook photo

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

Connecting to %s