If a menu pops up while typing, you must deal with that menu before continuing. 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?
You can add a shortcut 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 activator. Pressing that number selects the item:
Code: Select all
Menu, MyMenu, add, &%A_Index% %Item%, MenuAction
Code: Select all
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:
Code: Select all
TextMenu(TextOptions)
{
StringSplit, MenuItems, TextOptions , `,
Loop %MenuItems0%
{
Item := MenuItems%A_Index%
Menu, MyMenu, add, &%A_Index% %Item%, MenuAction
}
Menu, MyMenu, Show
Menu, MyMenu, DeleteAll
}
Add this MenuAction subroutine to the script:
Code: Select all
MenuAction:
SendInput, % Substr(A_ThisMenuItem,4) . A_EndChar
Return
Next, for each Hotstring menu, add the following TextMenu() function calling code—inserting the necessary Hotstring activator and replacement list separated by commas:
Code: Select all
::agin::
TextMenu("again,a gin,aging")
Return
Here’s the new script:
Code: Select all
::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