New Flexible Hotstring Features Added to AutoHotkey

Last February, AutoHotkey Release Included Important New Hotstring Capabilities

A few months back while working on my latest book, Jack’s Motley Assortment of AutoHotkey Tips, I encountered a new x option for Hotstrings. The x option enables the running of functions and commands rather than replacing text. This new feature surprised me since, in the past, a one-line Hotstring would only do text replacement.

I immediately recognized that this approach to one-line action Hotstrings could immediately shorten a long list of function Hotstrings. However, closer scrutiny of the new AutoHotkey release revealed further (and possibly more important) new Hotstring functionality. In addition to the x option, the February AutoHotkey release includes:

  1. A new Hotstring() function which adds dynamic capabilities to Hotstrings.
  2. A new method for creating function Hotstrings by defining the function immediately after the Hotstring(s).

Library Benefits

These added tools deserve special attention. In the next couple of blogs, I plan to examine the implications of these changes for AutoHotkey Hotstrings.

Shortening Hotstring Code with the X Option

One of the primary benefits of the Hotstring x option includes shortening AutoHotkey code. When we convert the following three-line Hotstrings (taken from the AddNextWeekDay.ahk script)—each of which calls a function:


we rewrite them as:


The x option disables the text replacement to the right of the second double colon and runs the function or command located in that spot. Now, working in a manner similar to Hotkeys, we can write a one-line action Hotstring by adding the x option.

Using an AutoHotkey Command in a One-Line Hotstring

Hotstring CommandWhen my kids were young, I led them to believe that I watched everything that they did on their computers via the network. This worked out pretty well until they figured out I had lied. If I had added a script similar to the one below to their computers, it would have reinforced my deception:

:x:heck::MsgBox, , MsgBox Pop-up,We don't use language like that!

Before AutoHotkey Release, AutoHotkey assumed that the text appearing on the same line as the Hotstring definition represented replacement text. To create an action Hotstring, you needed to start the commands on the following line. Rather than replacing text, the x option tells AutoHotkey to run the command or evaluate the expression.

Tip: Testing indicates that only the last Hotstring which executes the command requires the inclusion of the x option, although, for consistency, you may want to add it to each item in the stack.

Using Label Subroutines in Hotstrings with the X Option

While the x option in Hotstrings does not directly accept Label subroutine names, you can call any subroutine using the GoSub command:

:x:heck::GoSub, MsgBoxTest

  TextInput := substr(A_ThisHotkey,4)
  MsgBox, , MsgBox Pop-up,We don't use language like "%TextInput%" either!

This approach uses the single-line action feature to call another subroutine which manipulates the data before displaying it.

For the last Hotstring, we could have written the subroutine an action Hotstring but by creating a Label subroutine, rather than needing to stack the Hotstring definitions (as shown above), we can place our Hotstrings anywhere in the script—as long as, we use the GoSub command. This also helps us later when we create Hotstrings on-the-fly using the new Hotstring() function—a topic for next time.

Using Expressions in Hotstrings with the X Option

Although the documentation mentions using expressions in one-line Hotstrings, it did not include any examples. The following snippet offers a couple of statements where Hotstrings execute expressions:

:x:geez::MsgBox, % (xyz = "test")?"Yes " . xyz:"No " . xyz
:x:geez1::xyz := "test" . 1
:x:geez2::xyz := "test"

The first line forces an expression (% ) using the ternary operator (?:) which pops-up a message box whenever typing the text “geez” followed by a space or punctuation. Initially, the variable xyz contains no value. Type the keys “geez1” followed by a space and AutoHotkey stores “test1” to the variable xyz. Type the keys “geez2” and AutoHotkey stores “test” to the variable xyz. Type “geez” to check the result.

In effect, the above creates Hotkeys which activate when typing a series of text characters. You could always implement this type of action Hotstrings by starting the code on the line immediately following the definition. With AutoHotkey Release, we can now define it with one line of code. The significance of this techniques may become more apparent when we take a look at the new Hotstring() function which allows the creation, modification, and deletion of Hotstrings after loading the AutoHotkey script.

Major New Hotstring Features in AutoHotkey Release

I plan to dive deep into the Hotstring() function which adds power and flexibility to your Hotstring scripts not previously seen.

Click the Follow button at the top right of this page for e-mail notifications of new blogs.


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.)


3 thoughts on “New Flexible Hotstring Features Added to AutoHotkey

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