Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Trigger HotStrings by Special Keys


  • Please log in to reply
1 reply to this topic
Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Recently there were a number of wishes in the forum to trigger HotStrings by special keys, which currently cannot be put in the list of end-chars (#Hotstring EndChars ...). There have been many workarounds proposed. Here is another one, which requires only small changes in the HotString definitions (so you can have many) and accommodate very easily modified trigger keys, like Ctrl-Win-Alt-/:
Loop
{
   Input ch, I L1 V
   HOTY = %HOT%
   HOT  =
}

NumpadEnter::   ; use your favorite trigger key here
^Space::        ; just another example
   If (Asc(A_PriorHotKey) = Asc(":") and HOTY <> "")
      Send % "{BS " StrLen(A_PriorHotKey)-5 "}" HOTY
Return

:*B0:rtfm::
   HOT = read the fantastic manual
Return

:*B0:btw::
   HOT = by the way
Return
The HotStrings now don't require ending characters (*) and they don't erase the typed abbreviation (B0). They only assign the replacement text to the variable HOT.

There is an infinite loop, monitoring the characters typed. Whenever a new alphanumeric key is entered, the HOTY variable gets the value of the HOT variable, which, in turn, gets erased. It means that at the last key of a HotString the HOTY variable contains the active replacement text, but pressing any other alphanumeric key will move the already empty HOT variable to HOTY.

The desired end-characters are defined as HotKeys. They check if the previous HotKey was a HotString (their labels start with ":") and if the HOTY variable is not empty, that is, the end-char immediately follows the last HotString abbreviation. If yes, the abbreviation is erased with the right number of backspaces (5 more than the abbreviation, for the ":*B0:") and the replacement text, contained in HOTY, is sent to the active window.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
And here is an even simpler one, which works in editors or edit controls, where Shift-Ctrl-Left selects the word left of the insertion point. It may not work in every window and it uses the ClipBoard (which you can save to a variable and reload at the end), but its advantages are:
- it works even if you edited the last word
- it works if you moved the insertion point
- the definitions of HotStrings are single lines
$btw  = by the way             ; HotStrings: $abbreviation = replacement text
$rtfm = read the full manual

^Space::                       ; choose your favorite Trigger Key
   ClipBoard =                 ; empty the ClipBoard
   Send ^+{Left}^x             ; cut word on the left
   ClipWait 2                  ; wait until it gets to the ClipBoard
   $ := $%ClipBoard%           ; get replacement
   IfEqual $,,Send %ClipBoard% ; if none: send back cut word
   Else       Send %$%         ; send replacement
Return