The C Option (Case Sensitive)—An Easy Way to Limit Hotstring Action. Plus, How to Emulate Hotstring Capitalization in an Action Menu Subroutine.

* * *

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.

* * *

While working with the OverusedWords.ahk script from last time, I notice that particular issues arose when activating a replacement menu with the word have. For example, I found that have when used in the form of a question doesn’t need the pop-up menu. “Have you eaten yet?”
Regardless, the selection list of unnecessary and unwanteds replacement words appears. I don’t contemplate a time when in normal communications I would want to replace the word have at the beginning of a question. I prefer that the synonym menu not pop-up. There is a fairly easy fix for this.

By adding the C option to the Hotstring routine, it becomes case sensitive:

Adding the C option between the first two colons eliminates any Hotstring response to the capitalized form of the word. Questions starting with Have will no longer activate a pop-up menu.

This modified menu for the word “have” includes options for replacing the expression “have to.”

Note: I’ve also added a few more options to the have replacement menu (or more appropriately have to). While not appearing on the original list of overused words, have to is another expression which probably should be supplanted with one of the six items at the top of the menu at the right. (You may find more options to add to the have to list.)

No list of words is likely to include all possible replacements or usages of an overused word. You should tailor your lists in ways which suits you best.

Warning: Reinforcing what I wrote last time, the OverusedWords.ahk script should only be loaded when you’re doing your most significant development work. The script will slow you down as menus pop up at seeming random times. While leaving the script loaded all of the time may be educational, it could drive you insane.

A regular Hotstring (without the C option for case sensitivity) responds to any capitalization you may include when typing an activating string. For example, the following snippet yields different results depending upon whether you type btw, Btw, or BTW:

::btw::by the way

Type btw and you get by the way. Add an initial capital to Btw and By the way is returned. Utilize all capital letters by keying in BTW and the result BY THE WAY appears—also in all caps. This is a powerful automatic feature for regular hotstrings. No need to worry about whether you insert a word at the beginning of a sentence or in the middle. The initial cap is automatically carried through. This feature is lost in action Hotstrings running a snippet of code.

This problem exists for the Hotstring menus discussed here. Even if you capitalize the activating text, its replacement will display the all lowercase words from the menu.

One solution I considered was to add two case sensitive (C option) Hotstrings for each word: one with the menu items capitalized and the other all lowercase. This would work, however, since it requires adding two Hotstring setups for each overused word, it doubles the amount of code in the script.

I opted for the more complicated approach which is implemented in the OverusedWord.ahk script. It works by checking the first letter of the text for case, then appropriately capitalizing (or not) the replacement word. While this subroutine may seem a bit complicated for this application, it’s worth taking a glance at the techniques. You may want to accomplish something similar in other scripts. The WordMenuAction: subroutine is modified as follows:

As you can see, it is not a simple one-step process. I was hoping that there would be a Hotstring built-in variable which contains the activating string. That would make the problem much simpler. But since such a variable doesn’t appear to exist for AutoHotkey Hotstrings, I came up with the above code. (If anyone knows of a simpler solution, I all ears…or eyes. I’ll add any reasonable solutions to this blog.) After looking at this code, you may decide that the two sets of Hotstrings (one capitalized and one not) is more acceptable to you.

In essence, the above routine 1) captures the first character of the typed string, 2) checks for capitalization, then 3) returns the appropriate form of the the replacement text.

The first few steps select the first letter of the activating text. This is done in WordMenuAction: by deleting all but the first character before starting the screen selection process. The number of backspaces needed is one less than the value of the variable Backspaces and stored to the variable Check:

Check := BackSpaces -1

The BackSpace action is used to delete the last section of the screen text with the SendInput command:

SendInput {BackSpace %Check%}

The remaining first character is selected (highlighted) on the screen by holding down the Shift key, using the cursor to move one character to the left, then releasing the Shift key:

SendInput {Shift down}{Left}{Shift up}

This is effectively the same as clicking the left mouse button on one side of the character, dragging the mouse to the other side of the character, then releasing the left mouse button.

The previous contents of the Windows Clipboard is saved to the variable OldClipboard:

OldClipboard:= Clipboard

The Clipboard is emptied:

Clipboard:= ""

The selected first letter (now selected) is copied to the Clipboard:

Send, ^c ;cuts selected text

AutoHotkey waits for the text to load into the Clipboard:

ClipWait 0

If the Clipboard contents is uppercase (capitalized):

If Clipboard Is Upper

then, using the SubStr() function, the text from the menu item is parsed into the first letter (First) and remainder of the string (Second):