AutoHotkey Tip of the Week: Add Temporary Hotkeys to MsgBox Windows—September 23, 2019

Isolate Hotkeys to Only Operate for an Open or Active Window

In the Weekly Tip, “IfWinActive Versus #IfWinActive“, I recommended isolating Hotkeys to specific windows. This time I offer a practical example.

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

The #IfWinExist directive offers a number of advantages when creating temporary Hotkeys:

  1. The Hotkeys activate only when AutoHotkey opens the controlling window.
  2. If other conflicting Hotkeys exist, the temporary Hotkeys take precedence as long as the window exists.
  3. After closing the controlling window, the Hotkeys deactivate minimizing interference with other possible Hotkeys or shortcuts.

MultiPasteNoSpaceLight Bulb!After last week’s menu shortcut tip, I looked at the problem of adding multiple Hotkeys to the MultiPaste.ahk script. Unlike menus, AutoHotkey offers no simple method for adding single-key shortcuts to a MsgBox. While the Hotkey which runs the Input command works, it requires first hitting the Hotkey combination, then the selection of a number from the keyboard. By adding temporary Hotkeys to the script, a paste operation only require one Hotkey combination. However, we only want these Hotkeys available when the always-on-top MsgBox appears.

First, I used the #IfWinExist directive to isolate the Hotkeys to the MsgBox window:

#IfWinExist MultiPaste
  !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]
#IfWinExist

Library Benefits

Each Hotkey uses the item number in combination with the ALT key (ALT+1, ALT+2, ALT+3,…ALT+0). I could have used single-key numbers:

#IfWinExist MultiPaste
  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]
#IfWinExist

While this code creates single-key shortcuts, the snippet also prevents the use of the number keys with the MultiPaste MsgBox open.

In order to make the script portable for adding to a master script, I needed to add the title Multi Paste to the MsgBox command:

MsgBox, 4096, Multi Paste , % "[1] " . Transaction[1] . "`r" 
. "[2] " . Transaction[2] . "`r" 
. "[3] " . Transaction[3] . "`r" 
. "[4] " . Transaction[4] . "`r" 
. "[5] " . Transaction[5] . "`r" 
. "[6] " . Transaction[6] . "`r" 
. "[7] " . Transaction[7] . "`r" 
. "[8] " . Transaction[8] . "`r" 
. "[9] " . Transaction[9] . "`r" 
. "[0] " . Transaction[10]

Note: I added the space to the title to prevent confusion between the MsgBox window and the open script in Notepad. I also needed to add the intervening space to the title in the #IfWinExist directive:

#IfWinExist Multi Paste

The posted MultiPaste.ahk script contains all of these changes.

Extra Tip: You can also replace the #IfWinExist line with this format:

#If WinExist("Multi Paste")

To avoid Hotkey conflicts, I recommend you use this technique to isolate them to an active/existing window whenever possible.

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 Hotkey Tips, Tricks, and Techniques. (Chapter Three clears up the confusion between AutoHotkey’s IfWinActive command and the #IfWinActive directive.)

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

One thought on “AutoHotkey Tip of the Week: Add Temporary Hotkeys to MsgBox Windows—September 23, 2019

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