AutoHotkey Quick Tip: Conditional Hotstrings Using the Input Command

Sometimes the Input Command Solves a Sticky Hotstring Problem

* * *

I promised an associative array tutorial for my next blog, but this topic intervened. I should have the tutorial ready for next Monday.

* * *

Recent comment on my blog “Auto-Capitalize the First Letter of Sentences“:

This routine can be handy, though the main issue is that, in AHK, “if more than one hotstring matches something you type, only the one listed first in the script will take effect”. Actually, I’m not sure about that, as it appears that using the asterisk as a hotstring option takes precedence. In any case, this does seem to have the effect that other hotstrings that could otherwise be used cannot be used if they also match the code provided here. I have lots of those types of hotstrings, which I use on new lines and after punctuation. I have been unable to find any solutions to this since AHK does not accommodate sequential firing.

A short demo:

:T:tn::Thanks
:C*?:`nt::`nT

Run this script. Type “Tn” on a new line. It works to type “Thanks”. Now type “tn” on a new line. The first line in the script is now skipped. You see only “Tn” in response.

Comment On “AutoHotkey Tip of the Week: Auto-Capitalize the First Letter of Sentences” by mikeyww

Whenever a Hotstring fires, it resets and waits for the next Hotstring. Therefore, the firing of the new line character and “t” (`nt) removes the letter “t” from the next string and starts over with the “n” character. The asterisk option (*) does not affect this behavior. It merely causes that Hotstring to fire before the first—then reset—prior to pressing the “n” key. However, I do appreciate the problem.

Everywhere else typing “tn” inserts “Thanks” but a new line plus the letter “t” (`nt) disables the replacement by not recognizing the letter “t” as part of the new string. You can fix this by creating a temporary Hotstring using the Input command that responds to the “n” key:

:T:tn::Thanks
:C*?:`nt::
	SendInput, `nT
	Input, UserInput, T5 L1 C, , n
	if (UserInput = "n")
	    SendInput, hanks
Return

Now, whenever pressing “t” after a new line, the Input command waits five seconds to receive a lowercase “n” from the keyboard. If pressed, the conditional inserts “hanks”—the remainder of the target word. Any other key terminates the Input command without further action.

Other comments about the same blog refer to issues when using Hotstrings for the auto-capitalization of new paragraphs. Possibly AutoHotkey users could modify the above code to resolve similar problems.

* * *

For more information on using the Input command with Hotstrings, see the book, Beginning AutoHotkey Hotstrings for Creative AutoCorrection, Text Expansion and Text Replacement in:

Chapter Sixteen: “Create Instant Temporary AutoHotkey Hotstrings with the Input Command: For Those Times When All You Need for Special Tasks Is Temporary Hotstrings, Use the AutoHotkey Input Command.”

  • The AutoHotkey Input Command.
  • How the Input Command Works.
  • No True Value Found.
  • State Abbreviation Text Replacement.
  • A Quirk of the Input Command.
  • Other Uses of the Input Command.

Chapter Seventeen: “Reduce Code and Increase Power with the AutoHotkey Input Command: Use the Input Command to Eliminate Code While Creating Multiple Hotstrings. You’re Going to Like This One! It Includes Arrays and Hiding Data in Substrings.”

  • Converting the Script for the Input Command
  • AutoHotkey Variable Arrays
  • Storing Data in Text Strings
  • Take It to the Next Step

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.)

jack

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.)

Find my AutoHotkey books at ComputorEdge E-Books!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!

3 thoughts on “AutoHotkey Quick Tip: Conditional Hotstrings Using the Input Command

  1. Thanks for the reply, correction, and solution to the problem. It works, though not necessarily easy to scale to dozens or hundreds of hotstrings, and dozens of capital letters! I still appreciate your original script as well as this addendum.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s