Sometimes a Common Misspelling Has More Than One Possible Replacement Word. Here’s How to Add an AutoHotkey Pop-up Menu for Easy Word Selection.
* * *
As a convenience for people who want to get this entire series (plus more) in one place, I have now published the e-book Beginning AutoHotkey Hotstrings which is available on the ComputorEdge E-Books site (in three formats—EPUB for mobile devices, MOBI for Kindle, and PDF for printing and computer) and through Amazon. Regardless of whether you’re interested in the book or not, it’s worth your time to peruse some of the blogs linked at “Beginning AutoHotkey Hotstring Techniques” found under the “AutoHotkey Topics and Series” tab in the top menu bar. They just might inspire your next AutoHotkey script.
* * *
When looking over the AutoCorrect file which includes AutoHotkey Hotstrings for thousands of commonly misspelled words, there is a list of words offering numerous possible replacements. They appear in a non-active comments section toward the end of the AKH file because standard Hotstrings do not allow for options. A tiny bit of script writing is needed. Here is a sample of a few of those ambiguous misspellings excerpted from AutoCorrect:
::sheat::sheath, sheet, cheat ::shoudln::should, shouldn't ::sieze::seize, size ::siezed::seized, sized ::siezing::seizing, sizing ::sinse::sines, since ::snese::sneeze, sense ::sotyr::satyr, story ::sould::could, should, sold
If you want to allow the loading of one of these misspellings in AutoCorrect, the line must be moved outside the comment section of the file. But, even then, you must decide which option you prefer to use. With a regular Hotstring you can only pick one replacement.
When you make one of these typos, wouldn’t using a pop-up menu with all the options be a better alternative, thus allowing you to pick the best option?
Adding Menu Options to Hotstrings
AutoHotkey has a Menu command which makes it easy to create pop-up menus. By incorporating Menu commands into an action Hotstring, all selection options are instantly available whenever typing an ambiguous misspelling.
For example, the word agin is almost always wrong. But, the replacements include again, a gin, or aging. While in most cases again might be preferred, there could be times when a gin or aging is needed. The following AutoHotkey script creates a Menu which automatically pop-ups whenever the activating Hotstring agin is typed:
::agin:: Menu, MyMenu, add, again, MenuAction Menu, MyMenu, add, a gin, MenuAction Menu, MyMenu, add, aging, MenuAction Menu, MyMenu, Show Return MenuAction: SendInput %A_ThisMenuItem% Return
The first line of code is the activation text ::agin:: (the typo agin is automatically deleted when followed by a space or punctuation).
Each of the next three lines after ::agin:: adds an item to a pop-up Menu named MyMenu. The fourth parameter is both the name of the Menu item (again, a gin, or aging), as well as, the text displayed in the actual menu (shown above). The fifth and last parameter (MenuAction) is the Label subroutine called whenever a Menu item is selected.
The Menu, MyMenu, Show line tells AutoHotkey to display MyMenu and waits for input. The Menu pops up at the location of the mouse cursor.
When a selection is made, the Label subroutine MenuAction: runs. The only line in the subroutine uses the SendInput command (SendInput %A_ThisMenuItem%) to replace the original text with the contents of the built-in Menu variable A_This_Menu_Item—which contains the Menu item name. (Use the ESC key or click the mouse outside the menu to move on without making a selection.)
To add another AutoCorrect Menu, repeat the same code with the new options as shown:
::buring:: Menu, MyMenu, add, burying, MenuAction Menu, MyMenu, add, burning, MenuAction Menu, MyMenu, add, bring, MenuAction Menu, MyMenu, add, burin, MenuAction Menu, MyMenu, add, during, MenuAction Menu, MyMenu, Show Return
The original Label subroutine may be used over and over again with any number of selected misspellings. The only problem is that the action Hotstring will keep adding the new items to the original unneeded MyMenu making it longer for each misspelling menu activated. The MenuAction: subroutine needs to be modified to remove the previously loaded menu items:
MenuAction: SendInput %A_ThisMenuItem% Menu, MyMenu, DeleteAll Return
The new line of code in MenuAction: (Menu, MyMenu, DeleteAll) removes all previously added options leaving an empty menu ready for the next multiple option AutoCorrect.
Looking at the above snippets of action Hotstrings it’s quickly seen that when any number of new AutoCorrect menus are added, many lines of code will be needed. Next time, rather than writing tons of redundant code, we will put the Hotstring menu building code into a function which will work with any of the misspellings. Then each action Hotstring will be reduced to two lines of code (a word list and the function) reusing the same Menu building function.
There is also the issue of the automatic deletion of the activating Hotstring. There may be times when we want to ignore all of the menu options and continue with the original typed input. This makes using an AutoHotkey user-defined function even more important.
* * *
Find Jack’s AutoHotkey e-Books at ComputorEdgeBooks.com.
4 thoughts on “Make Your Own Text AutoCorrect Hotstring Pop-up Menus with AutoHotkey, (Part 4, Beginning Hotstrings)”
Hi, quick question: Is it possible to change where the menu appears in the screen so that instead appearing where the mouse pointer is hovering the menu appears where I’m typing it?
It’s because I found a little bit annoying, because usually my mouse pointer is in one place in the screen and I’m typing elsewhere in it.
Simply add the text cursor location variables to the Show subcommand:
Use the cursor keys to move in the menu and Return to select. Of course, you can always use the cursor keys and Return without touching the mouse regardless of where the menu is located.
Hello. I noticed a little sorta of bug with your script (the “Hotstring Menus for Multiple Options” – also, congratulations, great tool!). But about the bug: depending from where you type, especially if you’re typing something inside an html text box in a browser, the replacement function doesn’t quite work, and the script ends up putting the replacement word in the middle of the old word (instead of deleting the old word completely), here’s a gif explaining better:
:x*:amor\::HotstringMenu(“tenderness | &1==love | &2==fondness | &3==baby| &4”)
HotstringMenu(“tenderness | &1==love | &2==fondness | &3==baby| &4”)
Anyway, just a little suggestion, you probably will know how to come up with a better fix. Again, thank you, your script is awesome!
Web pages can react in strange manners—often too slow for an AutoHotkey script. It could be that the script needs some strategically placed Sleep, 200 commands (inside and/or at the start of the function?) to prevent the script from outrunning itself. That may be what your first Send command is doing. Not sure about the blank menu item.