Add Single-Letter Shortcuts to Your AutoHotkey Menus, Plus AutoCorrect Menus

Put simple Tips and Tricks that are not entire Tutorials in this forum
User avatar
jackdunning
Posts: 126
Joined: 01 Aug 2016, 18:17
Contact:

Add Single-Letter Shortcuts to Your AutoHotkey Menus, Plus AutoCorrect Menus

19 Sep 2019, 08:33

Sometimes It’s Just Easier to Use the Keyboard Rather Than Your Mouse

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?

Image

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
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:

Code: Select all

MenuAction:
  SendInput, % Substr(A_ThisMenuItem,4) . A_EndChar
Return
Image

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
}
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:

Code: Select all

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:

Code: Select all

::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:

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
Last edited by jackdunning on 19 Sep 2019, 11:07, edited 1 time in total.
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Add Single-Letter Shortcuts to Your AutoHotkey Menus, Plus AutoCorrect Menus

19 Sep 2019, 10:19

Nice, Jack.

Just a heads up - the smart quotes are breaking the script as posted.

Code: Select all

  TextMenu(“again,a gin,aging”)
needs to be

Code: Select all

  TextMenu("again,a gin,aging")
.
Regards,
burque505
User avatar
jackdunning
Posts: 126
Joined: 01 Aug 2016, 18:17
Contact:

Re: Add Single-Letter Shortcuts to Your AutoHotkey Menus, Plus AutoCorrect Menus

19 Sep 2019, 11:08

burque505 wrote:
19 Sep 2019, 10:19
Nice, Jack.

Just a heads up - the smart quotes are breaking the script as posted.

Code: Select all

  TextMenu(“again,a gin,aging”)
needs to be

Code: Select all

  TextMenu("again,a gin,aging")
.
Regards,
burque505
Thanks, corrected.
User avatar
jackdunning
Posts: 126
Joined: 01 Aug 2016, 18:17
Contact:

Re: Add Single-Letter Shortcuts to Your AutoHotkey Menus, Plus AutoCorrect Menus

19 Sep 2019, 15:21

I should note that you can replace the three-line action Hotstring with one line by using the relatively new X option:

Code: Select all

:x:agin::TextMenu("again,a gin,aging")

Return to “Tips and Tricks (v1)”

Who is online

Users browsing this forum: No registered users and 5 guests