Create Instant Hotstrings Using the AutoHotkey Hotstring() Function

While Building Temporary Hotstrings Using the AutoHotkey Hotstring() Function, We Learn DropDownList GUI Control Techniques

In Section 12, “Instant Hotkey GUI and Menu Section” of my book Jack’s Motley Assortment of AutoHotkey Tips, I go through a number of different techniques for keeping track of impromptu Hotkeys created with the InstantHotkey.ahk script (e.g. two-deep variables, associative arrays, etc). I thought of using that script as a model for this new InstantHotsting.ahk script. However, I might offer more learning points about other aspects of AutoHotkey if I start over with a new approach to this slightly varied problem.

Library Benefits

The AutoHotkey Hotstring() Function

I find the AutoHotkey Hotstring() function fairly straightforward. It allows the creation and alteration of Hotstrings on-the-fly after loading the script. Previously, the AutoHotkey scriptwriter needed to make any Hotstring changes in the original file, then reload it for them to take effect. This new function represents a boon for regular users of Hotstrings.

As a demonstration script, I’ve started working on an app for initiating instant Hotstrings. In the next series of blogs, I plan to explore various aspects of the Hotstring() function:

  1. Setup a barebones InstantHotstring.ahk script for creating and editing temporary Hotstrings—the subject of this blog.
  2. Add a feature for disabling/enabling specific temporary Hotstrings.
  3. Implement controls for adding and changing Hotstring options.
  4. Save and restore sets of temporary Hotstrings to and from files.

Interestingly, keeping track of the newly created Hotstrings, rather than the Hotstring() function itself, represents the greatest challenge in this script. If I don’t separately maintain a list of each new addition, I see no easy method to remember and edit all these new Hotstrings. After creating a Hotstring, even the (ListHotkeys command) which opens the Hotkeys and their methods section of the Main Window does not display the new entity. (I did see a script which appears to dig through the active source files to find active Hotkeys and Hotstrings, but it seemed a little too involved.) Tracking each Hotstring at time of creation makes the most sense.

In this first barebones Hotstring building script, I explore using the DropDownList GUI control to manage a list of all my temporary Hotstrings.

Using the DropDownList GUI Control to Track and Edit Hotstrings

It makes sense to use the DropDownList control as a list since it can display each Hotstring while offering a method for accessing it. (You may find this approach less complex than some of those I used with the InstantHotkey.ahk script.) However, the DropDownList control does come with some quirks which need special attention and scripting.

Note: As requested, I include the entire barebones InstantHotstring.ahk script at the end of this blog.

Create the InstantHotstring.ahk GUI (Graphical User Interface)

In this first step (and the auto-execute section of this InstantHotstring.ahk script), we build a GUI interface for the app.

InstantHotstringBarebones
Using the “Set Hotstring” button (hidden under the DropDownList control), we create new temporary Hotstrings while adding each to the list. To edit a Hotstring, select it from the DropDownList control.

The interface needs an Edit field for the Hotstring (NewString), a replacement text Edit field (TextInsert), a DropDownList control for tracking and editing the new Hotstrings (StringCombo) which activates the subroutine ViewString to recall the listed Hotstrings, and a Button control (gLabel subroutine AddHotString) for setting up and/or changing each Hotstring:

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, Show, , Instant Hotstring

This Instant Hotstring GUI uses many of the techniques discussed in both the online documentation and many of my books. Of note, the DropDownList control statement includes the Sort option and the AltSubmit option. Sort maintains the list in alphabetical order based upon the Hotstring activating text regardless of whether a new or updated Hotstring. When including AltSubmit, AutoHotkey stores the selected number of the DropDownList control item in the StringCombo variable rather than the item’s named text.

Adding a New Hotstring

While you’ll find adding the new Hotstring via the Hotstring() function easy (only one appearance in this script), working with the DropDownList control takes a little more code. Some techniques which work with other controls, such as ListView, require a little more effort here. For example, you won’t find an easy command for renaming a DropDownList item.

AddHotstring:
  Gui, Submit, NoHide

; Trap for blank Hotstring
  If (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
    }
  }

; 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

If we attempt to update a DropDownList item with new text, AutoHotkey adds a new DropDownList entry rather than updating the original. Therefore, to change an item’s text, we must first delete the original entry. To delete it with Control, Delete, [Item Number], ComboBox1, we need to know its numeric location on the list (Item Number).

If we use the AltSubmit option with the DropDownList control, then, rather than the item text, AutoHotkey saves the numeric location in the associated variable. However, this acts as an either/or proposition making it awkward to access the other data with the Gui, Submit command.

But first, to prevent redundancy, we must capture and search the entire DropDownList list for a matching Hotstring. We capture the contents of the entire DropDownList using the ControlGet command:

ControlGet, Items, List,, ComboBox1, A

Then we review each DropDownList control item with:

Loop, Parse, Items, `n

If the Hotstring exists in the list, we locate the target item with:

If InStr(A_LoopField, ":" . NewString . "::", CaseSense)

If found, we delete that DropDownList item, the Break the Loop:

Control, Delete, %A_Index% , ComboBox1
Break

Next, we insert the new/changed Hotstring definition into the DropDownList:

GuiControl,, ComboBox1 , % "::" . NewString . "::" . TextInsert

Set the DropDownList control selection to that new/changed Hotstring:

GuiControl, ChooseString, ComboBox1, % "::" NewString "::" TextInsert

The Hotstring

We set up the Hotstring using the Hotstring() function:

Hotstring("::" . NewString , TextInsert, "On")

The first parameter of the Hotstring() function includes the required double-colon plus the Hotstring text (NewString). This allows for later adding the options to the function by inserting them between the two colons (e.g. :B0C:test::this is a test). The function assumes the second set of double-colons.

The first time AutoHotkey runs the Hotstring() function it defaults to enabled (even without the “On” parameter). While the function does not require the “On” parameter when first setting up the Hotstring, whenever editing a current Hotstring, including it will automatically re-enable a previously disabled Hotstring.

Note that some statements use the Control command while others use the GuiControl command. The Control command “makes a variety of changes to a control” while the GuiControl command “Makes a variety of changes to a control in a GUI window.” Control can work in any Windows program which uses controls recognized by Window Spy, while GuiControl only works for an AutoHotkey GUI. Although you will find some overlap, the two commands offer significantly disparate features. Take the time to review those differences.

Selecting Hotstrings for Changes

We want to load the appropriate text into the two Edit fields when we select an item in the DropDownList control:

ViewString:
  ControlGet, Select, Choice ,, ComboBox1, A
  HotString := StrSplit(Select, ":")
  GuiControl, , Edit1, % HotString[3]
  GuiControl, , Edit2, % HotString[5]
Return

The ControlGet command uses the Choice subcommand to copy the text found in a selected DropDownList item:

ControlGet, Select, Choice, , ComboBox1, A

Similar to Control and GuiControl, AutoHotkey offers both a ControlGet and GuiControlGet command. Normally, we could retrieve the same text with:

GuiControlGet, Select, , ComboBox1, A

However, since we used the AltSubmit option with the DropDownList control, the above GuiControlGet command returns the numeric position of the item rather than text. You’ll find it worthwhile to review the different capabilities of ControlGet and GuiControlGet. If one doesn’t do the job, the other just might.

We parse the text into its component parts by creating an array of its elements (Hotstring[i]):

HotString := StrSplit(Select, ":")

We load the appropriate text into each Edit field:

GuiControl, , Edit1, % HotString[3]
GuiControl, , Edit2, % HotString[5]

Select a Hotstring from the DropDownList control and the ViewString subroutine loads the selected text into the associated Edit fields.

No Options Codes Added—Yet!

This barebones 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 the coming blogs, I’ll add features for disabling and enabling Hotstrings, implementing Hotstring options (e.g. case sensitivity) for the temporary Hotstrings, and saving and restoring the Hotstrings to and from a file—all while exploring more of the idiosyncrasies of the Hotstring() function.

Click the Follow button at the top right of this page for e-mail notifications of new blogs.

InstantHotstring.ahk (Barebones)

The following includes the complete barebones InstantHotstring.ahk script:

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, Show, , Instant Hotstring

Return

ViewString:
  ControlGet, Select, Choice ,, ComboBox1, A
  HotString := StrSplit(Select, ":")
  GuiControl, , Edit1, % HotString[3]
  GuiControl, , Edit2, % HotString[5]
Return

AddHotstring:
  Gui, Submit, NoHide
  If (NewString ="")
  {
    MsgBox Enter a Hotstring!
    Return
  }

  ControlGet, Items, List,, ComboBox1, A
  Loop, Parse, Items, `n
  {
    If InStr(A_LoopField, ":" . NewString . "::", CaseSense)
    {
      Control, Delete, %A_Index% , ComboBox1
      Break
    }
  }

  GuiControl,, ComboBox1 , % "::" . NewString . "::" . TextInsert
  GuiControl, ChooseString, ComboBox1, % "::" NewString "::" TextInsert
  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.)

 

 

10 thoughts on “Create Instant Hotstrings Using the AutoHotkey Hotstring() Function

  1. Jack, That hotstring() function really helped me accomplish what I wanted when I wrote InfoWarp.

    Now, I’m just contending with dang antivirus warnings with some of my apps that were fine before. Not compressing helps but I think I’ll just start creating installers that contain the script and the AutoHotkeyU32 executable to launch them with. That U32 exe only shows one warning on VirusTotal whereas compressed InfoWarp.exe shows 17 warnings!!! I like having some program code protection but since it’s all freeware, I guess it doesn’t matter for me to compile.

    Check my new blog article out when you can. I spent considerable time seeing if there are any other script languages that can do most of what AutoHotkey can, and be less volatile with antivirus programs. Heck, I even noticed just yesterday that browsing to my Blog site with Microsoft Edge browser threw up a warning. I filled out their form saying I own the site and it doesn’t contain malware. I just tried that browser again and saw no warnings. I’m glad that worked.

    Script Languages vs Antivirus Programs | Michael’s Tech Notes – Blog

    | | | | | |

    |

    | | | | Script Languages vs Antivirus Programs | Michael’s Tech Notes – Blog

    |

    |

    |

    Appreciate all your articles!

    Thanks, Mike

    Like

Leave a Reply to InstantHotstring.ahk AutoHotkey Scripts – ComputorEdge Software Showcase Cancel 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 )

Facebook photo

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

Connecting to %s