Turn AutoHotkey System Tray Icons into One-Click Action Buttons (AutoHotkey Tip)

Rather Than Using Hotkey Combinations, a Simple Click of the Icon in the Windows System Tray Launches Your App

One of the features I added to the InstantHotkeyArrays.ahk script turns the Windows System Tray AutoHotkey into a one-click button for launching new Instant Hotkeys. The new hot-button made from an existing icon works out well—especially since I can never remember the Hotkey combination for creating a new Instant Hotkey.

You’ll find it relatively simple to turn any of your AutoHotkey Tray icons into an action button. The Menu command includes a two-step procedure for activating the one-click icon button:

  1.  Set the target Menu item as default in the Windows System Tray icon right-click menu.
  2.  Set the Tray icon to the one-click action mode.

The Main Window

One-click Icon
When set to one-click mode, a left-click of the icon launches the default menu item (in bold) in the AutoHotkey Systems Tray icon menu.

Normally, when you double-click an AutoHotkey Tray icon, the Main Window opens—the same Main Window that pops up when you select Open from the icon right-click menu.

Note: While you’ll find many references to the Main Window in the online documentation, you won’t find a specific definition. For our purposes, the Main Windows gives us a peek inside the inner workings and hidden mechanism of a running AutoHotkey script (definitely a topic for a blog in the near future). Normally, the Main Window only opens in a non-compiled script (.ahk). To add the feature to a compiled script (.exe), insert the Tray menu  MainWindow option:

Menu, Tray, MainWindow

Once you set a default menu item,  a double-click on the AutoHotkey Tray icon switches to running that item’s action:

Menu, Tray, Default, Instant Hotkeys

Any time you use the Menu, [MenuName], Default, [MenuItemName] option, the default menu item appears in bold (as shown in the image above). However, it only responds to a mouse double-click when appearing in the top-level Tray menu. All submenus and non-System Tray menus merely display the item in bold characters with no other effect.

Once you set the default menu item, change the mouse double-click to a single-click as follows:

Menu, Tray, Click, 1

This turns the AutoHotkey Tray icon into a hot-button—one click and the action starts.

In the InstantHotkeyArrays.ahk script, this technique works great for the first click. However, after the first activation, when we add the submenu to the Instant Hotkeys menu item, we lose any icon single-click action. To restore the mouse-click hot-button, we add the New Instant Hotkey menu option to the main Tray menu and set it as the default:

If IH_Count = 1  ; only for the first activation
{
Menu, Tray, Add, Instant Hotkeys, :HotkeyMenu
Menu, Tray, Insert, Instant Hotkeys, Add Text, :InsertText
Menu, Tray, Icon, Add Text, InstantHotkey.ico, 1
Menu, Tray, Add, New Instant Hotkey, AddNewHotkey
Menu, Tray, Icon, New Instant Hotkey, C:\Windows\System32\shell32.dll, 319
Menu, Tray, Default, New Instant Hotkey
}

Once AutoHotkey sets the new default (New Instant Hotkey), clicking the icon continues launching a new Instant Hotkey GUI for each click.

New Instant Hotkey Menu
The new code adds a new Tray menu item for launching New Instant Hotkeys, then sets it as default.

If an AutoHotkey icon takes up space in the Windows System Tray, you may as well make it do something. This saves you from memorizing one more Hotkey combination.

Unfortunately, AutoHotkey limits you to one hot-button per icon. In merged documents, only the last one-click icon button works. Since AutoHotkey only allows one default menu item, the last one set up in the script responds to the icon click.

jack

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s