Add Action to Your Hotstrings Using the New X Option (AutoHotkey Tip)

The Hotstring X Option Offers More Power by Running Commands, Functions, and Subroutines, Plus How to Temporarily Block External Hotstrings

In February of last year, the powers-that-be added a new Hotstring() function and a number of other Hotstring related features (See “New Flexible Hotstring Features Added to AutoHotkey.”) This major change added a host of new possibilities for creating and manipulating Hotstrings. The Hotstring() function acted as the impetus for my InstantHotstring.ahk script. In the process of writing that app, I developed a better understanding of how to enhance Hotstrings. Getting immediate feedback when implementing new replacements and options allowed me to quickly investigate many possibilities.

For example, I activated my EmojiInsert.ahk file by loading it into the InstantHotstring.ahk app. Admittedly, it took a couple of minutes for the script to load and activate all the emoji Hotstrings. The file includes over 1000 AutoHotkey Hotstrings for inserting emoji characters into any Windows document or Web page. I watched as AutoHotkey parsed each Hotstring, loaded the code into the appropriate fields, then activated the auto-replacement using the Hotstring() function. (I probably should add a filter to the script for loading only the Hotstrings I need. Or, I could break up the emoji file by category into separate Hotstring scripts. Either approach would save time.)

instanthotstring emoji
Notice that when reviewing the emojis in the DropDownList control many of the Hotstrings show only a nondescript square rather than the actual emoji. Probably due to the shortcomings of the Windows DropDownList, many of the extended emojis only display a square. However, once selected (and whenever executing an emoji Hotstring), the proper symbol appears.

Library BenefitsOne of the new features which definitely deserves further investigation includes the Hotstring X option which changes the replacement text into an executable command, Label subroutine name, or function name. When adding a Hotstring to a script, you can use the X option to force AutoHotkey to interpret the replacement text as one of the three. A little more limited, the Hotstring() function only interprets Label names and functions.

Comparing the Usage of the Hotstring X Option in Scripts Versus the Hotstring() Function

The capacity to execute actions with AutoHotkey Hotstrings is not new. In the past, starting a routine on the line following the Hotstring and terminating it with the Return command has acted as the go-to Hotstring action method. You can perform any custom action with this technique. In version 1.1.28+, AutoHotkey added a number of new methods for introducing action into Hotstrings—including Function Hotstrings. But, you’ll find the new Hotstring X option the most flexible for adding script subroutines and functions to your Hotstrings.

Script Embedded Hotstring Definitions

With the X option, you can now add an AutoHotkey command or expression directly to a Hotstring definition. However, you’ll find a significant difference between how you can implement the X option for a Hotstring definition written into a script and for the Hotstring() function.

Note: With both in-script Hotstring statements and the Hotstring() function, a Label subroutine or function must first exist within the script when using the X option to call either of the two.

When coded into an AutoHotkey script, the X option allows you to directly call commands and expressions on the same Hotstring line:

:X:wer::click()        ; Calls the function click()
:X:asd::gosub, click1  ; Go to subroutine the Label click1:

The first Hotstring evaluates the previously defined function click():

  MsgBox,,, Click function!, 5

which pops up a MsgBox displaying the text “Click function!”—timing out after 5 seconds.

The second Hotstring jumps to the previously defined subroutine click1:

  MsgBox,,, Click Label!, 5

which pops up a MsgBox displaying the text “Click Label!”—timing out after 5 seconds.

The Hotstring X Option in the Hotstring() Function

Once I added the above function and subroutine to the InstantHotstring.ahk script, I could call either by creating a temporary Hotstring.

When using the X option in the Hotstring() function, you can only run existing Label or function names—no commands or expressions.

Hotstring(":X:wer","click")    ; Calls the function click()
Hotstring(":X:asd","click1")   ; Go to the Label name click1:

The Label names and function names must appear without the colon or parentheses.

Using the Hotstring X Option in the InstantHotstring.ahk Script

In the InstantHotstring.ahk script, we test the sample Label subroutine we added to the script in a new Hotstring using the X option:

instanthotstring click1

After typing the Hotstring “asd” followed by a space, the MsgBox “Click Label!” pops up.

In the InstantHotstring.ahk script, we test the sample function we added to the script in a new Hotstring using the X option:

instanthotstring click

After typing the Hotstring “asd” followed by a space, the MsgBox “Click function!” pops up.

In each of the above cases, we used the Hotstring() function example code shown in the previous section.

Using Temporary Hotstrings to Block Other Hotstrings

In a previous blog, I discussed the possibility of using the Hotstring() function to temporarily block Hotstrings running in separate scripts. This proved more difficult than I originally thought. While I could temporarily override the external Hotstring, any reset I issued within the script did not affect the other Hotstring.

For example, if I created the Hotstring:


to block this Hotstring running in another script:

::btw::by the way

it worked fine on the first activation by replacing “btw” with “btw” rather than the original text. But, if I hit the space bar for a second time (or another punctuation key), “bby the way” popped in. I needed a Hotstring reset method which works across all running AutoHotkey scripts. I accomplished this universal reset by scripting a simulated mouse click which automatically resets all Hotstrings—regardless of the script:

  Send, % SubStr(A_ThisHotkey,4)
  Click, %A_CaretX%, %A_CaretY%
  Send, % A_EndChar

After cropping out the activating Hotstring, SubStr(A_ThisHotkey,4), AutoHotkey uses the Send command to replace the temporary Hotstring text. Next, AutoHotkey clicks the mouse, Click, %A_CaretX%, %A_CaretY%, at the text cursor location thereby resetting all Hotstrings in all running scripts. Finally, AutoHotkey appends the EndChar using Send, % A_EndChar.

I added the Reset: subroutine to the InstantHotstring.ahk script. Now, if I encounter a Hotstring replacement occurring at an awkward time, rather than modifying or disabling a running script, I can use the InstantHotstring.ahk script to temporarily block it.

For example, my main autocorrect script converts naive into naïve—placing an umlaut over the ï. Suppose I don’t want to look too pretentious when writing a particular message? I can temporarily block the change:

instanthotstring reset

By adding the Label subroutine Reset: to the script, I can now temporarily block any external Hotstring by using the identical Hotstring, adding the X option, and inserting Reset (the subroutine) as the replacement text.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)


This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)


Occasionally, some of your visitors may see an advertisement here,
as well as a Privacy & Cookies banner at the bottom of the page.
You can hide ads completely by upgrading to one of our paid plans.


One thought on “Add Action to Your Hotstrings Using the New X Option (AutoHotkey Tip)

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