Adding Actions to Windows System Tray Icon Menu (Intermediate AutoHotkey Tip)

Adding Hard-to-Remember Hotkeys to the AutoHotkey System Tray Icon Makes Sense—Plus, How to Best Activate the Last Window

I’ve put my MousePrecise AutoHotkey script on my own list of favorites. I like the mouse cursor accuracy it adds to any of my graphic applications. I find it particularly useful when selecting a portion of an image for cropping or moving.

However, I recently ran into an issue where the script stopped working in Paint.NET (more details on the source of the problem later in this blog). I tried reloading the script, but to no avail. I decided to use the Hotkey I implemented for adding the window class to the group (GroupAdd command). Humm…I couldn’t recall the key combination. This lapse of memory forced me to open the script and dig through the code.

That’s the problem with Hotkeys. If you use too many of them, recalling each challenges your memory. You can create a list of Hotkeys for displaying in a pop-up help window, but even then you need to memorize that key combination. I decided to solve this issue by adding the Hotkey action to the MousePrecise.ahk System Tray icon menu.

*         *          *

New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”

*         *          *

Adding Hotkey Action to the System Tray Menu Icon

As a best practice, you should probably make a habit of adding important Hotkey actions to the System Tray icon menu. If you use particular Hotkeys often you might remember those ones, but the infrequently executed key combinations tend to fall into the nether realms of the brainpan.

An icon menu entry helps in two ways. First, it executes the Hotkey actions. Second, if you include the key symbols in the menu listing, it acts as a visual reminder of the Hotkey combination. In its simplest form, the following Menu command adds a Help item to the icon menu:

Menu, Tray, Add, Help (F12), F12
systemtrayhelpmenuitem
A “Help (F12)” menu item appears on the System Tray icon right-click menu. Select to open the help pop-up window.

After setting up a Hotkey (F12::) in the script to display the help pop-up window, this line of code adds “Help (F12)” to the System Tray icon menu as an option. Right-click on the AutoHotkey icon to open the menu. You immediately see the proper key combination—even if you don’t select the menu item. While the Help (F12) selection opens a predefined help window, it also acts as a great Hotkey reminder (F12). By adding the actions to the menu along with the associated key combination names (F12), we’ve eliminated the need for a help pop-up window altogether.

To assign the Hotkey to the menu option, include the key combination symbol(s). Remember, the last parameter in the Menu code above (F12) represents a label (subroutine) or submenu. After defining a Hotkey, the combination also acts as a label (subroutine) name available for use with other AutoHotkey commands. In the example above, selecting the menu item Help (F12) invokes the F12 Hotkey subroutine.

For most Hotkeys, adding the key combination as the label does the job. However, if the subroutine needs to work on the active window, using the icon menu presents a common AutoHotkey problem.

Activating the Last Window from the System Tray Icon Menu

When selecting an item from a System Tray menu, the original window loses focus leaving it inactive. When recalling the Hotkey label from the menu, any action depending upon an active last window no longer works. Therefore, another label which returns focus to the original window before calling the Hotkey must be created.

systemtrayiconmenu
CTRL+NumpadDel appears in the System Tray icon menu as a reminder, but the called routine first activates the last window by sending the Windows ALT+ESC shortcut.

For example, executing the add “another window class” routine in MousePrecise.ahk requires an active target window. Since the System Tray activates at the time of the menu selection, the Hotkey won’t add the class of the last window to the group. We must insert a new subroutine which activates the last window first with the ALT+ESC Windows shortcut, then executes the Hotkey. The Menu command uses a new label named AddClass for the subroutine:

Menu, tray, add, Add Window Class (CTRL+NumpadDel), AddClass

Here we add in the label (subroutine) AddClass to return to the on-top window before running the Hotkey:

AddClass:
  SendInput, !{Esc}
  GoSub, ^SC053  ; Hotkey called
Return

After a great deal of research, I’ve found that ALT+ESC (SendInput, !{Esc}) key combination offers the simplest, yet the most reliable, technique for jumping to the last window. While other methods exist for finding the on-top window, they get too complicated and present other issues.

Finding the last window highlights a common source of confusion in AutoHotkey. In previous scripts, I’ve included methods for guaranteeing that the Hotkey action occurred in the proper window by using the WinActivate command. This works fine if you start the action while within that window, but the mere action of selecting the System Tray menu deactivates the original window. We must return to that window before proceeding with the subroutine—in this case, adding the window class to the group.

Although WinActivate, without any parameters, generally returns to the last window found, it’s not that simple in this situation. Another AutoHotkey window command must have run previously in that window for it to work.

There are two Windows shortcuts which return to the on-top window: ATL+TAB and ALT+ESC. The ALT+TAB  combination works, but, it delays the action by first opening the window preview selection view. That time lag dictates the addition of the Sleep command—allowing a pause long enough for the previous window to activate.

Sending the ALT+ESC Windows shortcut (SendInput, !{Esc}) immediately jumps to the on-top window. The only problem noted with ALT+ESC occurs when another window sits always-on-top.

Why MousePrecise Stopped Working in Paint.NET

I was a little perplexed when I noticed that the MousePrecise.ahk script no longer worked in Paint.NET. I had hard coded the Paint.NET class into the script with the GroupAdd command. However, I could temporarily re-add the class with the Hotkey combination as discussed above. I finally broke out my WindowProbe.ahk script to check Paint.NET’s class. It had changed!

Then, I remembered that I had recently updated the program. I only needed to update the AutoHotkey window class code in the script. It turns out that the class of a program can change—especially if you update to a new version of the software.

jack

*          *          *

This week I took a close look at Nutcache Project Management in the ComputorEdge Software Showcase. If you manage collaborative tasks or merely need to bid jobs or bill clients, then Nutcache offers a powerful, yet free, system.

*          *          *

If you find Jack’s AutoHotkey Blogs useful, then please consider contributing by purchasing one or more of Jack’s AutoHotkey books. The books make handy AutoHotkey references.

Advertisements

4 thoughts on “Adding Actions to Windows System Tray Icon Menu (Intermediate AutoHotkey Tip)

  1. “That’s the problem with Hotkeys. If you use too many of them, recalling each challenges your memory.”

    How about this? You pause and rack your brain for that keystroke command that you used just last week…It’s gotta be…let’s try… Contol+alt+/…Oops! Undo! Undo!

    This is exactly why I developed the AHK Enterpad. Each of its dedicated and labelled hotkeys can be used to launch a custom script without having to remember or guess anything.

    Since then, the AHK Enterpad is a good way for me to promote AutoHotkey to people who may benefit from using plenty of (generally short) AHK scripts.

    Denis Lamarre, Cedeq

    Like

  2. Hi Jack,

    Talking of ‘Adding Actions to Windows System Tray Icon Menu’, perhaps you would consider writing about using – for example – ‘ToggleCheck, MenuItemName’ to amend a running script dynamically, i.e. change something (like toggling logging to a file vs a MsgBox) then reload.

    Go on, you know you want to… :)

    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 )

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