Using the AutoHotkey Hotstring() Function to Disable/Enable Hotstrings

While the Hotstring() Function Won’t Delete Hotstrings, It Can Toggle Them Off and On (Disable and Re-Enable)

Technically, once created, you can’t delete a Hotstring without exiting the script. However, you can disable and enable its function. When you disable a Hotstring using the Hotstring() function, it loses its power—similar to the effect of deleting it. But, since the disabled Hotstring still exists, you can bring it back to life by re-enabling it with the same Hotstring() function.

Library Benefits

I can see situations where someone might want to temporarily create an instant Hotstring, use it for a while, then disable it—later re-enabling it again. Yet, you might discover other reasons for creating instant Hotstrings which toggle on-and-off.

Altering External Hotstrings

The Hotstring() function only works directly on Hotstrings running in the same AutoHotkey script. That means you can’t change Hotstrings in your AutoCorrect script with an InstantHotstring.ahk script. Or, can you?

Since AutoHotkey responds to the most recently set up Hotstring, you can override or block any other running Hotstring (in or out of the script) with a temporary Hotstring. For example, suppose I want to block the Hotstring:

::btw::by the way

found in my always-loaded AutoCorrect file. By adding the temporary Hotstring:

::btw::btw

it supersedes the previous “btw” Hotstring and replaces the string with itself. You can use this to block almost any current Hotstring. But, first, we alter the InstantHotkey.ahk script to turn our interim Hotstrings on and off.

Note: You’ll find this version of the InstantHotkey.ahk script at the end of this blog. I’ve posted the latest version of the script at the ComputorEdge Free AutoHotkey Scripts page.

Curious Tip: In the course of testing this Hotstring blocking technique, I found that hitting a terminating key (e.g. spacebar or other punctuation) more than once caused the external Hotstring to fire. In fact, if I set up similar Hotstrings in various other scripts, with each terminating key press, AutoHotkey cascaded through all of those active replacements—although, leaving remnants of each previous replacement. I believe that using the Z option to reset the Hotstring will eliminate this problem but I leave that for another time when I deal with adding Hotstring options to the InstantHotstring.ahk script. (January 23, 2019 Update: I believe I’ve found a solution to this problem. You can find it at the end of the blog “Add Action to Your Hotstrings Using the New X Option (AutoHotkey Tip).”)

Toggling Hotstrings On and Off

To take advantage of the disable/enable capability in the Hotstring() function, I added a new GUI button:

Gui, Add, Button, gToggleString ys, Toggle Hotstring On/Off

Clicking this button runs the ToggleString gLabel subroutine causing the selected Hotstring in the DropDownList to toggle off or on.

Hotstring Toggle Button

The gLabel subroutine ToggleString includes the code necessary to get the job done. Since AutoHotkey initially enables any Hotstring, the DropDownList needs to clearly show that the specific Hotstring no longer works. We do this by adding the stop sign emoji (🛑) and the word “Stopped!” to the listing. Plus, for further clarification, we alter the toggle button text to “🛑Enable Hotstring.”

Hotstring Toggle Off

As discussed in the last blog, the DropDownList control does not allow the changing of the text in an individual item. We must first delete the original and insert a new item.

The Choice subcommand for the ControlGet command retrieves the text from the currently selected item in the DropDownList control:

ControlGet, Select, Choice ,, ComboBox1, A

We plan to delete this item for later replacement, but first, we capture the current values in an array (Hotstring[i]) using the StrSplit() function. This allows us to continue without losing track of Hotstring data:

HotString := StrSplit(Select, ":")

Before we can delete the item, but we need its number in the DropDownList control. Since the original Gui, Add, DropDownList statement included the AltSubmit option, the following statement uses the GuiControlGet command to retrieve the item number rather than the text—storing it to the variable Pick:

GuiControlGet, Pick ,, ComboBox1

Use the Control, Delete command to remove item number Pick from the DropDownList:

Control, Delete, %Pick% , ComboBox1

We can now toggle the Hotstring and add it back to the DropDownList control.

The Hotstring Toggle Routine

The Hotstring() function offers three options for disabling/enabling Hotstrings:

Hotstring(String , Replacement, OnOffToggle)

We use “Off” (or 0) to disable a Hotstring, “On” (or 1) to enable a Hotstring, or “Toggle” (or -1) to switch the current state. I choose to use the positive action of “Off” and “On” since, in other similar routines, I’ve occasionally lost track of the current state when using the “Toggle” option. (I did not find an easy method for testing the disabled/enabled state of a Hotstring, so I opted for precise control rather than using the “Toggle” parameter.)

Marking the Current Disable/Enable State

I use the text in the DropDownList control item to tell me the current setting for the Hotstring. If it includes the stop sign emoji (🛑), then the script must enable it. If not, the script must disable the Hotstring:

If InStr(HotString[5], "🛑")
{
  GuiControl,, ComboBox1 , % "::" . HotString[3] . "::"
       . SubStr(HotString[5],1,-11) . "||"
  GuiControl,, Button2 , Disable Hotstring
  Hotstring("::" . HotString[3] , SubStr(HotString[5],1,-11), "On")
}
Else
{
  GuiControl,, ComboBox1 , % "::" . HotString[3] . "::" 
        . HotString[5] . "🛑 Stopped!||"
  GuiControl,, Button2 , 🛑 Enable Hotstring
  Hotstring("::" . HotString[3] , HotString[5], "Off")
}

By using an emoji to distinguish between enabled and disabled Hotstrings, we reduce the possibility that our temporary replacement string (TextInsert) will confuse the script. While the emoji could conceivably appear in any Hotstring replacement, we severely lower the odds of an error.

Note: If the script uses emojis, save the script as Unicode. I’ve added the stop sign emoji (🛑) to this script to highlight disabled Hotstrings. However, for the technique to work, I must save the file as Unicode. Programs such as Notepad warn you if you attempt to save Unicode data in a non-Unicode (or ANSI) file. Otherwise, you might lose your symbols.

The first time we click the Toggle button, the Else portion of the conditional triggers.

The first two GuiControl statements add the disabled Hotstring to the DropDownList control appending the text “🛑 Stopped!”—then, correspondingly, altering the text in the toggle button:

GuiControl,, ComboBox1 , % "::" . HotString[3] . "::" 
      . HotString[5] . "🛑 Stopped!||"
GuiControl,, Button2 , 🛑 Enable Hotstring

Note that the two vertical lines at the end of the GuiControl statement (||) do not appear in the DropDownList control item text. Adding those characters defaults the DropDownList control to this newly added item.

The Hotstring() function for disabling merely adds the word “Off” as a third parameter:

Hotstring("::" . HotString[3] , HotString[5], "Off")

Note, since we don’t want to affect the Hotstring structure, we make no changes to the TextInsert parameter (Hotstring[5]).

When disabled, the conditional re-enables the Hotstring and removes the “🛑 Stopped!” warning from the DropDownList control:

GuiControl,, ComboBox1 , % "::" . HotString[3] . "::"
       . SubStr(HotString[5],1,-11) . "||"
GuiControl,, Button2 , Disable Hotstring
Hotstring("::" . HotString[3] , SubStr(HotString[5],1,-11), "On")

To clean up the disable warning, we must remove the last 11 characters from the end of Hotstring[5] (the TextInsert variable):

SubStr(HotString[5],1,-11)

We must do the same in the Hotstring() function since we don’t want to change the enabled HotString:

Hotstring("::" . HotString[3] , SubStr(HotString[5],1,-11), "On")

We do not want the disabling/enabling subroutine to affect the Hotstring itself. Therefore, whenever selecting another Hotstring in the DropDownList control, the script must remove any warnings before loading the TextInsert field.

View Hotstring Selection

To prevent jumping to a different Hotstring (making a new selection in the DropDownList) from adding the disable warning to the TextInsert field, we add the following conditional to the ViewString subroutine (discussed in the last blog):

If InStr(HotString[5], "🛑")
{
  GuiControl,, Button2 , % "🛑 Enable Hotstring"
  GuiControl, , Edit2, % SubStr(HotString[5],1,-11)
}
Else
{
  GuiControl,, Button2 , Disable Hotstring
  GuiControl, , Edit2, % HotString[5]
}

This prevents changing Hotstring selections in the DropDownList control from affecting the Hotstring itself. Regardless of whether or not AutoHotkey has disabled a Hotstring, the raw data in the NewString and TextInsert fields remain unchanged.

Resetting Re-Enables Hotstring

Since clicking the Set Hotstring button always enables the Hotstring, this action removes any disabled status and starts fresh. We don’t need additional code in the AddHotstring subroutine. The TextInsert field used by the subroutine contains only the original replacement string.

Next, Adding Options Codes

This barebones-plus-disable-button InstantHotstring.ahk script only creates basic Hotstrings—no options—for temporary use while running the script—nothing saved. In most cases, this may be all you’ll need. In an upcoming blog, things get more complicated when I add controls for implementing Hotstring options (e.g. case sensitivity, etc) for the temporary Hotstrings—while exploring more of the idiosyncrasies of Hotstring options and the Hotstring() function. Later, I add saving and restoring the Hotstrings to and from a file to the list of blogs.

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

The InstantHotstring.ahk Script with Disabling Feature

The following script includes the complete working code from the last blog with the disable/enable routines incorporated:

InstantHotstringSetup:

Gui, Font, s12, Arial
Gui, Add, Text, , Enter Hotstring 
Gui, Add, Edit, w50 vNewString ys, 
Gui, Add, Text, xs, Enter Replacement Text 
Gui, Add, Edit, w400 vTextInsert xs, <Enter Hotkey Text>
Gui, Add, DropDownList, w400 vStringCombo xs gViewString Sort AltSubmit,
Gui, Add, Button, gAddHotstring section, Set Hotstring
Gui, Add, Button, gToggleString ys, Toggle Hotstring On/Off
Gui, Show, , Instant Hotstring

Return

ToggleString:

If (NewString ="")
{
  MsgBox Create and set Hotstring!
  Return
}
ControlGet, Select, Choice ,, ComboBox1, A

HotString := StrSplit(Select, ":")

GuiControlGet, Pick ,, ComboBox1
Control, Delete, %Pick% , ComboBox1

If InStr(HotString[5], "🛑")
{
  GuiControl,, ComboBox1 , % "::" . HotString[3] . "::"
      . SubStr(HotString[5],1,-11) . "||"
  GuiControl,, Button2 , Disable Hotstring
  Hotstring("::" . HotString[3] , SubStr(HotString[5],1,-11), "On")
}
Else
{
  GuiControl,, ComboBox1 , % "::" . HotString[3]
      . "::" . HotString[5] . "🛑 Stopped!||"
  GuiControl,, Button2 , 🛑 Enable Hotstring
  Hotstring("::" . HotString[3] , HotString[5], "Off")
}

Return

ViewString:

ControlGet, Select, Choice ,, ComboBox1, A
HotString := StrSplit(Select, ":")
GuiControl, , Edit1, % HotString[3]
GuiControl, , Edit2, % HotString[5]
If InStr(HotString[5], "🛑")
{
GuiControl,, Button2 , % "🛑 Enable Hotstring"
GuiControl, , Edit2, % SubStr(HotString[5],1,-11)
}
Else
{
GuiControl,, Button2 , Disable Hotstring
GuiControl, , Edit2, % HotString[5]
}
Return

AddHotstring:

Gui, Submit, NoHide
; Trap for blank Hotstring
If (Trim(NewString) ="")
{
  MsgBox Enter a Hotstring!
  Return
}

; Delete target item from list
ControlGet, Items, List,, ComboBox1, A
Loop, Parse, Items, `n
{
  If InStr(A_LoopField, ":" . NewString . "::", CaseSense)
  {
    Control, Delete, %A_Index% , ComboBox1
    Break
  }
}
GuiControl,, Button2 , Toggle Hotstring On/Off

; Add new/changed target item to list
GuiControl,, ComboBox1 , % "::" . NewString . "::" . TextInsert

; Select target item in list
GuiControl, ChooseString, ComboBox1, % "::" NewString "::" TextInsert

; Create Hotstring and activate
Hotstring("::" . NewString , TextInsert, "On")
Return

 

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

4 thoughts on “Using the AutoHotkey Hotstring() Function to Disable/Enable Hotstrings

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 )

Google photo

You are commenting using your Google 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