Sending Multiple Saved Data Items to Documents and Forms in AutoHotkey (Temporary Hotkeys)

Sometimes We Want Single-key Hotkeys to Work Only for Short Periods of Time

The press of a single key provides the easiest method for inserting data into an edit field or document. In AutoHotkey, you can activate any key as that quick action single-key with either a Hotkey or Hotstring. However, in the normal course of work, that technique renders that keyboard action useless for anything else. To get the convenience of one-key instant activation, we must activate that Hotkey when needed—only in specific circumstances and for short periods of time.

AutoHotkey offers a number of different methods for accomplishing this instant key action. Which we choose depends upon what we want to do. In this blog, I look at three different methods:

  1. Use the Hotkey command to temporarily turn Hotkeys on then off again.
  2. Use the #If directive to designate conditional Hotkeys.
  3. Temporarily pause the script after Hotkey activation, then deactivate Hotkeys upon resuming.

While each temporary Hotkey works in a similar manner—activating only when needed—each has its advantages and disadvantages.

Hotkey Command Turns Temporary Hotkeys On and Off

Traditionally, we add Hotkeys to a script using the double-colon format. When the script loads, the Hotkeys exist for the duration of the run. The Hotkey command allows the creation of new Hotkeys on-the-fly and later turn them off. This dynamic, flexible method allows the activating of Hotkeys only when needed—depending upon conditions:

In the MouseMeasureMultiLeg.ahk script discussed in “Measure Multiple Line Segments with an AutoHotkey On-Screen Ruler,” when finalizing the journey (press the Shift key during the DrawStuff subroutine), AutoHotkey Loops through the array of legs to set up temporary Hotkeys each inserting individual leg distances into documents or edit fields:

loop, % Array_count	{
	MsgLine := MsgLine . "Leg " A_Index ": " MyArray[A_Index].length " Total: " MyArray[A_Index].total " " units "`n"
	If A_Index < 10
		Hotkey, % "$" A_Index , DistanceSend, On
		}
		MsgBox, 4096, Multi-Leg Mouse Measure, % MsgLine
Return

The Hotkey command (line 4) uses the Loop index (A_Index) to create a single-key Hotkey calling the DistanceSend subroutine. For simplicity, the code limits the number of Hotkeys to nine (1-9).

Tip One: If your script requires more than nine Hotkeys, you can switch to alphabetic characters using techniques similar to those found in “Auto-Capitalize the First Letter of Sentences” for Hotstrings using Chr(A_Index + 64) to convert to the letter keys.

Tip Two: Include the On switch in the initial Hotkey command. While a Hotkey automatically activates when first created, after turning the Hotkey Off later in the clearing portion of the script, the reactivating Hotkey command must include On when reusing it for a new multi-leg measurement.

Important Note: Since the DistanceSend subroutine uses the Send command (SendInput), as a rule, you should add the $ option to the Hotkey. The dollar sign option prevents the refiring of the Hotkey when the Send command outputs one of the single-key Hotkeys. I occasionally forget this condition—causing me to wonder why I get such strange results—especially in this script where every digit in the numeric output can force the activation of another Hotkey.

The DistanceSend subroutine uses the Hotkey itself to determine which array element to Send to the document:

DistanceSend:
	keyvar := substr(A_ThisHotkey,2,1)
	SendInput,  % MyArray[keyvar].Length
Return

This subroutine uses SubStr() function (line 2) to extract the second character from the pressed Hotkey (e.g. $1, $2, etc). This value (keyvar) designates which array element to insert into a document or edit field.

Later, when cleaning up the MouseMeasure route, a similar Loop turns all current Hotkeys Off:

loop, % Array_count	{
     If A_Index < 10
        Hotkey, % A_Index , , Off UseErrorLevel
	ToolTip, , , , % A_index + 1
}

The UseErrorLevel option skips the warning dialog and prevents an error (such as a non-existent Hotkey) from suspending stript processing when the Hotkey command attempts to turn Off an unavailable Hotkey. This may occur after the deletion of journey legs temporarily leaving excess array elements in the Array_Count.

Use the #If Directive to Designate Conditional Hotkeys

In the MultiPaste.ahk script discussed in “Add Temporary Hotkeys to MsgBox Windows,” I use the #If directive to activate a set of Hotkeys only when the MsgBox window exists. Although each Hotkey exists at all times, the entire group only activates temporary when the message box exists.

When the “Multi Paste” message box window opens, all ten of the single-digit Hotkeys activate—top row numbers only—not the numeric keypad. The message box sits always-on-top as a reminder that the keys continue in the active mode. Dismissing the window returns the number keys to their normal duties.

#If WinExist("Multi Paste")
  $1::SendInput, % Transaction[1]
  $2::SendInput, % Transaction[2]
  $3::SendInput, % Transaction[3]
  $4::SendInput, % Transaction[4]
  $5::SendInput, % Transaction[5]
  $6::SendInput, % Transaction[6]
  $7::SendInput, % Transaction[7]
  $8::SendInput, % Transaction[8]
  $9::SendInput, % Transaction[9]
  $0::SendInput, % Transaction[10]
#If

On the plus side, these Hotkeys instantly activate and deactivate without any further coding. On the downside, as fixed Hotkeys, they lose a certain level of flexibility.

For more information, see “Chapter Two: Block Windows Shortcuts with AutoHotkey” and “Chapter Three: AutoHotkey #Directives for Context-Sensitive Hotkeys—#IfWinActive” of the book AutoHotkey Hotkeys: Tips, Tricks, and Techniques.

Temporarily Pause the Script after Hotkey Activation, Then Deactivate Hotkeys upon Resuming

In some situations, stopping script processing after activating Hotkeys will do the job. However, unlike the first two methods discussed—which work in most circumstances—only a couple of AutoHotkey commands stop script processing long enough to allow interaction with the Hotkeys. In particular, the InputBox command and MsgBox command.

Unless the script requires specific input (i.e. InputBox) to direct the course of the next steps, MsgBox provides the command of choice. By default, when a message box displays, the AutoHotkey script stops running until closing the window. The message box offers an opportunity to inform the user about the Hotkey actions and—if set to always-on-top—acts as a continuous reminder that the activated Hotkeys exist.

The following short example sets up three Hotkeys, waits while displaying the MsgBox always-on-top, then turns Off the Hotkeys after the user closes the window:

Hotkey, $1 , NumberSend, On
Hotkey, $2 , NumberSend, On
Hotkey, $3 , NumberSend, On

MsgBox, 4096, Spell Number,    ; Stops script processing!
(
1 — One
2 — Two
3 — Three
)

; InputBox, test
Gui,Add,Text,text
Gui,show

Hotkey, $1 , , Off
Hotkey, $2 , , Off
Hotkey, $3 , , Off

Return

NumberSend:
	keyvar := substr(A_ThisHotkey,2,1)
	If keyvar = 1
		SendInput, One
	If keyvar = 2
		SendInput, Two
	If keyvar = 3
		SendInput, Three
Return

As expected, AutoHotkey offers a number of different methods for creating temporary Hotkeys. Which you choose depends entirely upon what you need your script to do.

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“!

4 thoughts on “Sending Multiple Saved Data Items to Documents and Forms in AutoHotkey (Temporary Hotkeys)

  1. Great explanation and will read more in-depth. Is there a simple way to have a hot key do one thing first time and a second time something else and basically flip back and forth? I’m trying to get my mouse wheel to select a complete sentence and then move one arrow to the right next using same key and then select next sentence. This will help me replace compete text or insert in between them or infront or behind using the scroll wheel. Do you happen to have a working example or approach?
    Ps ruler working like a charm!

    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 )

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