Sometimes It’s Just Easier to Use the Keyboard Rather Than Your Mouse
If a menu busts in while typing, it forces you to switch to your mouse for resolution. This can get pretty annoying if your script uses a number of pop-up menus. For example, Chapter Eight, “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” of the book Beginning AutoHotkey Hotstrings shows you how to set up a list of alternative corrections. It works well for offering options but, at times, wouldn’t you prefer to hit a single key to make the selection rather than first fetching the mouse, then clicking?
Recent Question from a Reader:
Is there any way to improve the script in order to, once the menu appears, select an option using a given key combination?
For instance: If I typed “alt+1” AutoHotkey would automatically select the option “again”, if I typed “alt+2” it would select the option “a gin” and so on so forth until alt+0?
* * *
Yes, you can add a hot character to each menu item by inserting an ampersand before a number (or another character). The ampersand turns the following character into a single-key shortcut. Pressing that number selects the item:
Menu, MyMenu, add, &%A_Index% %Item%, MenuAction
Adding the built-in variable A_index to the text adds sequential digits to the menu items—up to nine (1-9). However, since the script uses the text in the menu item for replacement, you will need to remove the ampersand and number from the menu item before replacing the word:
MenuAction:
SendInput, % Substr(A_ThisMenuItem,4) . A_EndChar
Return
Adding Multiple Options to AutoCorrect
In the AutoCorrect.ahk script, you’ll find a list of hundreds of Hotstrings (commented out) which offer more than one alternative replacement. A regular Hotstring only replaces a misspelling with one option. By using this Hotstring menu technique, you can activate multiple items in a pop-up menu. You only need to add one function (TextMenu()) and one action Label subroutine (MenuAction:) to your script.
The TextMenu() function parses the list of options using the StrSplit() function and inserts them into the menu:
TextMenu(TextOptions)
{
StringSplit, MenuItems, TextOptions , `,
Loop %MenuItems0%
{
Item := MenuItems%A_Index%
Menu, MyMenu, add, &%A_Index% %Item%, MenuAction
}
Menu, MyMenu, Show
Menu, MyMenu, DeleteAll
}
Anywhere after the auto-execute section of the script, place this function in your AutoCorrect.ahk file. It’s usually safe to insert it toward the end of the file.
Add this MenuAction subroutine to the script:
MenuAction:
SendInput, % Substr(A_ThisMenuItem,4) . A_EndChar
Return
For convenience, place this Label either before or after the above function.
Next, for each Hotstring menu, add the following TextMenu() function calling code—inserting the necessary Hotstring activator and replacement list separated by commas:
::agin:: TextMenu("again,a gin,aging") Return
This snippet—a different one for each multiple selection Hotstring—can appear almost anywhere in the AutoCorrect.ahk script. I keep them together in alphabetical order—similar to the original inactive list. This makes them easier to review and modify in the future.
Here’s the new script:
::agin:: TextMenu("again,a gin,aging") Return TextMenu(TextOptions) { StringSplit, MenuItems, TextOptions , `, Loop %MenuItems0% { Item := MenuItems%A_Index% Menu, MyMenu, add, &%A_Index% %Item%, MenuAction } Menu, MyMenu, Show Menu, MyMenu, DeleteAll ;Moved from MenuAction: } MenuAction: SendInput, % Substr(A_ThisMenuItem,4) . A_EndChar Return
For more details about how this Hotstring technique works, see the book Beginning AutoHotkey Hotstrings.
Regular Expressions (RegEx) Trick: To save retyping each sample Hotstring in the AutoCorrect.ahk file, I used Ryan’s RegEx Tester to convert the list to the proper Hotstring format.

I used the Regular Expressions (RegEx):
::(.+?)::(.+?)\n
capturing key data in each Hotstring. I used the following replacement text (including the hidden returns) for each item:
::$1:: TextMenu("$2") Return
I then remove excess space characters by matching and replacing any comma plus one space “, ” with a lone comma “,”—thereby removing the extra spaces from the calling functions.
This saved a ton of time.
For more information on writing effective AutoHotkey Regular Expressions (RegEx), see A Beginner’s Guide to Using Regular Expressions in AutoHotkey: Exploring the Mysteries of RegEx.
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.)
I wish there was a normal accelerator key feature for menus in AutoHotkey so I could set the amp before the letter in the text menu I want to use as accelerator key e.g. &again to use ‘a’ and the letter shall be underlined in the menu text to highlight it
LikeLike
You can do that. Just remove the ampersand before inserting the text:
LikeLike