AutoHotkey Tip of the Week: Use the GUI Menu Bar for Instant Hotkeys

Rather than Creating New Hotkeys and Isolating Them Using the #IfWinActive Directive, Simply Add Keyboard Accelerators Using a GUI Menu Bar

Note: This week’s keyboard accelerator tip is not the same technique as adding an ampersand before any letter in a menu item—although that trick still works. The beauty of this technique lies in the fact that you can embed and execute a multitude of active window Hotkey-like actions in a menu bar without ever opening the menu itself.

In almost every one of my books, I discuss using the Menu command to build free-floating selection lists for a wide variety of tasks. I use menus for the HotstringMenu.ahk script, the QuickLinks.ahk favorites app, SynomynLookup.ahk, and numerous other purposes. In the GUI ListView control examples found in the AutoHotkey Applications book for the to-do list, the address book, the calorie counting app and listings of icon images, the right-click context menu pops open for individual action items. Menus provide an easy method for adding features to AutoHotkey scripts while saving space. I’ve always known that you can add a menu bar to the top of any AutoHotkey GUI (Graphical User Interface) but had little to say about it—until now.

ToDoListINI
The Ctrl+T in the menu bar item creates an accelerator key combination which, whenever pressed, acts as an active window Hotkey for instantly executing the option—even without opening the GUI menu bar.

While working on my latest to-do list script (ToDoListINI.ahk), I realized that I wanted to add a couple more options to the app, but I didn’t like the idea of inserting more buttons into the GUI. I naturally turned to a menu bar at the top of the GUI which could include many more actions while taking up minimal area. After a quick glance at the Gui, Menu command, I realized that I had routinely overlooked one of the most important aspects of Gui menu bars: accelerator keyboard combinations.

(In my defense, many of my scripts don’t use GUIs and those which do tend to act as barebones starter scripts with little complication and few features. However, I now confess that most GUI apps would greatly benefit from adding menu bars—if only for the availability of these accelerator key combinations.)

Anyone who uses Windows recognizes menu bar accelerator keys. When running an application, Ctrl+S usually saves your work to file, Ctrl+O opens a file, Ctrl+Z reverts back from the latest change, plus many more. When you open the menu bar, these key combinations (if any) appear on the right side of the menu item as shown in the image above. We use these key sequences all the time.

Library Benefits

Note: A number of these key combinations such as Ctrl+Z (undo) and Ctrl+A (select all) are built into the Windows system and don’t require a menu bar to work in an AutoHotkey GUI. But many more commonly used accelerator shortcuts, such as Ctrl+S and Ctrl+O, only register when added to a GUI Menu Bar.

GUI menu bars take up a minimal amount of space and allow a script to pack an enormous number of options into them. By facilitating the creation of instant menu bar shortcuts which add window-specific Hotkey-like accelerators without all the associated coding, AutoHotkey can set up a vast number of keyboard shortcuts—bypassing the menu bar. Inserting the GUI menu bar and accelerator keyboard combinations into AutoHotkey scripts embraces the essence of this week’s AutoHotkey tip.

Instant Menu Bar Hotkeys

Light Bulb!As I perused the Gui, Menu command, I realized that I could add “keyboard accelerators” (similar to Hotkeys) which work for any option in the menu bar. Gui menu bars operate in pretty much the same manner as any other menu except they allow the definition of key combinations for instantly launching the menu action.

When appended to the end of a MenuItemName, the tab character (`t) followed by one or more standard Hotkey modifiers (Ctrl, Alt, or Shift) plus another key “indicates a keyboard shortcut that the user may press instead of selecting the menu item. If the shortcut uses only the standard modifier key names Ctrl, Alt and Shift, it is automatically registered as a keyboard accelerator for the GUI.”

To do the same with the regular AutoHotkey Hotkeys would require that you define the Hotkey then contain it within the #IfWinActive directive to isolate it to the active window.

The following code creates and adds the menu bar shown in the ToDoListINI.ahk image above:

Menu, FileMenu, Add, E&xit`tCtrl+X, SavePosition
Menu, HelpMenu, Add, &Toggle Help ToolTips`tCtrl+T, ShowToolTip
Menu, MyMenuBar, Add, &File, :FileMenu 
Menu, MyMenuBar, Add, &Help, :HelpMenu
Gui, Menu, MyMenuBar

The snippet above adds two Hotkey-like accelerators to the menu bar: Ctrl+X (for exiting the app) and Ctrl+T (for toggling the help ToolTip messages on and off).

You can find the ShowToolTip toggling subroutine in the ToDoListINI.ahk script posted at the ComputorEdge Free AutoHotkey Scripts page.

Take It to the Extreme

MenuBarGUIIf you only want the benefit of these instant accelerator key combinations, then you can add a sole menu bar to an almost empty GUI. While you need to place at least one GUI control in a GUI for it to remain visible after loading, you can use a non-displaying empty Text control in the window. That way you create a launching platform with a tiny footprint consisting of numerous Hotkey-like keyboard combinations and menu items available only from the active GUI window. This saves writing extensive Hotkey definitions bound by the #IfWinActive directive. Plus, you can select the action directly from the menu item—which also acts as a key combination reminder. However, you should note the following keyboard accelerator nuances:

  1. Assigned menu bar keyboard accelerators do not take precedence over previously defined Hotkeys. Other Hotkeys continue to operate while blocking any accelerators using the same key combination.
  2. Assigned menu bar keyboard accelerators do take precedence over conflicting built-in Windows shortcuts such as Ctrl+A for selecting all and Ctrl+C for copying. A GUI window with menu bar accelerators only blocks the conflicting built-in Windows shortcuts while active.
  3. Accelerator combinations embedded in menu bar submenus work in the same manner as the top-level accelerators without opening the menu. Higher-level menu accelerators using the same conflicting key combination take precedence over submenu accelerators.
  4. This type of keyboard accelerator does not work for standard or context pop-up menus.

The AutoHotkey code for the above Menu Bar Gui contains one submenu in the Web Sites menu for demonstration purposes:

Menu, AppMenu, Add, Google Chrome`tCtrl+Alt+C, MenuTest
Menu, AppMenu, Add, Mail Program`tShift+Alt+M, MenuTest
Menu, AppMenu, Add, IrfanView`tAlt+Shift+I, MenuTest
Menu, AppMenu, Add, Paint.Net`tCtrl+Alt+P, MenuTest
Menu, AppMenu, Add, LibreOffice Write`tCtrl+Alt+W, MenuTest
Menu, AppMenu, Add, AuoHotkey CodeQuickTestert`tCtrl+Alt+Q, MenuTest
Menu, WebMenu, Add, AuoHotkey Site`tCtrl+A, MenuTest
Menu, HelpMenu, Add, Google`tCtrl+G, MenuTest
Menu, HelpMenu, Add, Web, :WebMenu
Menu, MyMenuBar, Add, Apps, :AppMenu 
Menu, MyMenuBar, Add, Web Sites, :HelpMenu
Gui, Menu, MyMenuBar

Gui, Add, Text ; added for GUI persistence
Gui, Show, w200

Return

MenuTest:
  MsgBox, % A_ThisMenuItem
Return

As a rule, I plan to add GUI menu bars to most of my GUI windows if only for the easy addition of Hotkey-like accelerator keys.

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

2 thoughts on “AutoHotkey Tip of the Week: Use the GUI Menu Bar for Instant Hotkeys

  1. Hi Jack, Great tip! Would have been good with one example for each bullet point 1 to 4 on the “nuances”. Thx for all good previous tips! Regards from Sweden.

    Like

Leave a Reply to Sören Giver 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 )

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