AutoHotkey Tip of the Week: AutoHotkey Hotstring Menus for Text Replacement Options

Put Hard-to-Remember AutoHotkey Hotstring Replacements in a Menu

Beginning AutoHotkey Hotstrings 200px

Light Bulb!

This tip expands on Chapter Eight of my book Beginning AutoHotkey Hotstrings, “Make Your Own Text AutoCorrect Hotstring Pop-up Menus with AutoHotkey” and Chapter Nine, “How to Turn AutoHotkey Hotstring AutoCorrect Pop-up Menus into a Function.” You can use these techniques in your AutoHotkey scripts to make the selection of similar Hotstrings easier to remember by selecting from a menu.

For example, recently a reader posed a question about using similar Hotstrings which only change an optional number following the keyword:

::flux::Flux
:*?:flux1::Flux {#}1
:*?:flux2::Flux {#}2

MenuFlux

The number of Hotstrings expands as the number of “Flux” statements grows. After including the TextMenu() function and the MenuAction action subroutine (both shown below), we create a Hotstring menu from which we can choose from any number of statements:

 :x:flux::TextMenu("Flux,Flux {#}1,Flux {#}2")

By using the Execute (X) Hotstring option, AutoHotkey calls the TextMenu(TextOptions) function to create the pop-up menu:

TextMenu(TextOptions)
{
  StringSplit, MenuItems, TextOptions , `,
  Loop %MenuItems0%
  {
    Item := MenuItems%A_Index%
    Menu, MyMenu, add, %Item%, MenuAction
  }
  Menu, MyMenu, Show
  Menu, MyMenu, DeleteAll
}

Selection of an item from the menu executes the subroutine MenuAction:

MenuAction:
  SendInput %A_ThisMenuItem%%A_EndChar%
Return

(I detail how these AutoHotkey snippets of code work in the book Beginning AutoHotkey Hotstrings.)

To activate any Hotstring menu, I merely need to assign the Hotstring activator and add the options to the TextOptions list:

 :x:flux::TextMenu("Flux,Flux {#}1,Flux {#}2")

while adding this function call to the same script. The {#} ensures that AutoHotkey sends the normally modifying hash mark # as a raw character.

Single-Key Action

MenuFluxShortcutIn his question, the reader wanted to add the Flux number (#1, #2, #3, etc) by merely hitting a number key. We can accomplish this by adding shortcuts to the menu using the ampersand (&):

:x:flux::TextMenu("Flux,Flux {#}&1,Flux {#}&2")

Now, after the menu pops up, pressing 1 inserts Flux #&1 and pressing 2 inserts Flux #&2. To remove the shortcut-creating ampersand (&), we modify the MenuAction subroutine as follows:

MenuAction:
  TextOut := StrReplace(A_ThisMenuItem,"&") ; Remove the &
  SendInput %TextOut%%A_EndChar%
Return

This causes AutoHotkey to insert the appropriate text after hitting the corresponding numeric key. (Note: Menu shortcuts are limited to single digits—in this case, 0-9.)

Cleaning Up Modifying Characters in Menus

If you don’t like the look of the curly brackets in the menu, them remove them from the calling function’s word list:

:x:flux::TextMenu("Flux,Flux #&1,Flux #&2")

and add the {raw} option to the MenuAction subroutine:

MenuAction:
  TextOut := StrReplace(A_ThisMenuItem,"&")
  SendInput {raw}%TextOut%%A_EndChar%
Return

This eliminates the special properties of the Hotkey modifiers such as the hash mark #. (Adding {raw} in this subroutine probably represents a more robust form of the command.)

Using Hotstring Menus with Special Characters

Originally, I used a separate Hotstring for each currency symbol:

:*?:cent*::¢
:*:pound*::£
:*:yen*::¥
:*:euro*::€

MenuMoneyWhile easy to remember, every unique symbol requires its own Hotstring. By placing the symbols in a Hotstring menu, hitting the backtick key after entering the dollar sign provides me all the options:

:x*?:$``::TextMenu("¢,£,¥,€")

MenuEmoji

However, depending upon your Windows setup, distinguishing symbols in a pop-up menu can get tricky as shown in the menu at the right. Next time, I’ll explore more options for eliminating ambiguity from menus.

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

jack

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 “AutoHotkey Tip of the Week: AutoHotkey Hotstring Menus for Text Replacement Options

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