This AutoHotkey GUI (Graphical User Interface) Trick Builds New GUIs (Graphical User Interfaces) On-the-Fly and in the Same Script without Conflicts
I originally wrote the InstantHotkey.ahk script as a practical demonstration of how to use the Hotkey GUI control. The script’s major downside involves the need to run a new instance of the app for each new Hotkey combination/insertion text pair. Adding the #SingleInstance Off directive solved the problem of loading the same script multiple times, but the System Tray clogged with icons after initiating a number of Instant Hotkeys simultaneously. (I couldn’t just eliminate the System Tray icons (#NoTrayIcon or Menu, Tray, NoIcon) because I could quickly lose track of the various Hotkeys. Plus, eliminating tray icons presents other dangers.)
I combined the script with my master AutoHotkey script by including a Hotkey and the Run command (^!h::Run, InstantHotkey.exe). This avoided the problem of multiple conflicting GUIs in the same script—although it did nothing about the System Tray icon overload.
My recent work inspired me to solve the problem by uniquely naming the GUIs. In the course of writing the new script, I added a number of other useful techniques which I plan to cover in future blogs. Stayed tuned! You might find a few of them quite helpful.
Running Multiple GUI Pop-ups in the Same Script
The secret to building multiple GUIs in the same script lies in the Gui, New command. Each time you run the Gui command with the New sub-command, AutoHotkey creates a distinct pop-up. “Calling Gui New ensures that the script is creating a new GUI, not modifying an existing one.”
While the New sub-command does create a new default GUI, it does not name or number it. To recall a specific GUI you need to either name it yourself or save its unique ID. “Use the +HwndGuiHwnd
option to store the HWND of the new window in GuiHwnd. Gui, %GuiHwnd%:Default
can be used to make the other Gui commands operate on it by default.” I chose to name the GUIs rather than use the HWND.

Note: Over the next couple of weeks (for demonstration purposes ), I’m adding new Instant Hotkey scripts to the ComputorEdge Free AutoHotkey Script page for specific educational purposes. I’m starting with InstantHotkeyTwoDeep.ahk to which I’ll later add a loop to replace the Two-Deep method for tracking Hotkey text. Next, I’ll convert the entire script to using associative arrays. All three scripts provide the same result while using different AutoHotkey techniques in their inner workings.
In the InstantHotkeyTwoDeep.ahk script, I use a pseudo-array with a sequential numeric variable (IH_Count) to name each new GUI (e.g. InstantHotkey%IH_Count%). This allows me to track each GUI window and later correlate it with System Tray menu items:
IH_Count := 1 GUIName := "InstantHotkey" . IH_Count Gui, %GUIName%: new ; Create a new GUI Gui, %GUIName%:Add, Text,, Enter Hotkeys (ALT, SHIFT, and/or CTRL plus Key) ; The following hidden edit field tracks the GUI/menu item number. Gui, %GUIName%:Add, edit, hidden ys vGuiNum,%IH_Count% Gui, %GUIName%:Add, Hotkey, vKeyCombo ys Limit1 Gui, %GUIName%:Add, Edit, w400 vTextInsert xs, Gui, %GUIName%:Add, Button, gSetupKey , Set Key Combo IH_Count++ ; Get ready for next new Instant Hotkey
With the exception of the unique GUI names and the line of code which introduces a hidden Edit field for tracking the assigned GUI number (vGuiNum), this code matches that of the original InstantHotkey.ahk discussed in AutoHotkey Applications.
Each time this snippet runs, it creates a new Instant Hotkey GUI using sequential names (e.g. InstantHotkey1, InstantHotkey2, InstantHotkey3, …). This not only allows multiple distinct pop-ups to exist in the same app but later makes integrating the script into a master app much simpler.
Displaying the new GUI requires a Gui, Show statement:
Gui, Show, , Instant Hotkey
This works fine for the default GUI when first created, but later AutoHotkey needs the GUI name to open the correct pop-up window. We make this possible by adding System Tray menu items which numerically coincide with the GUIs:
ShowHotkey: Gui, InstantHotkey%A_ThisMenuItemPos%:show Return
Since the position of the menu item always matches the number of the Instant Hotkey GUI, we concatenate that number to the GUI name prefix (InstantHotkey%A_ThisMenuItemPos%).

The InstantHotkeyTwoDeep.ahk script tracks the various GUIs through menus items accessed in the System Tray icon right-click menu. Coupling the menu item numbers with the GUI names allows easy tracking and access. I’ll leave the discussion of the new System Tray menu structure for next time.
Note: If you would like to march ahead on your own, the InstantHotkeyTwoDeep.ahk includes comments which may help guide you in your understanding of the script.
* * *
Like anybody else, I have expenses and a need to make ends meet. As “Jack’s AutoHotkey Blog” increases in popularity, coding the test scripts and writing the blogs takes up more of my time. That means I’ve less time to pursue other income-earning opportunities. I don’t plan to ever move “Jack’s AutoHotkey Blog” behind a paywall, but if you think my efforts are worth a bit of your hard-earned cash, then help support my efforts by purchasing one or two of my AutoHotkey books or making a direct contribution. You may not need the references yourself, but you might know someone who can benefit from them.
Thank You,
Jack Dunning