AutoHotkey Tip of the Week: Tricks for Tracking and Activating Target Process Windows

When Implementing Various AutoHotkey Techniques Sometimes We Need to Find Our Way Back to the Proper Window

Most AutoHotkey applications don’t require complicated tracking of process windows. Hotstrings, Hotkeys, and most other techniques do their work in the currently active window. Generally, menus and GUIs automatically return to the original on-top location once they close. However, occasionally circumstances force us to move to other apps or tools—deactivating the current window—before returning to the original target. AutoHotkey offers a couple of techniques for getting back to the right spot on our Windows Desktop.

piechartcartoonFor example, the MousePrecise.ahk app (for precision movement of the mouse cursor) allows users to temporarily enable the mouse accuracy tool in new windows with either a Hotkey combination or a right-click selection from the Windows System Tray icon menu. Since the Hotkey doesn’t change window focus, it works fine, but the act of selecting an item from the System Tray icon menu deactivates the original window. Therefore, before AutoHotkey can include the new app in the active group, it must refocus on that process window and capture its title.

Library BenefitsWhile the WinActivate command can restore the original window, it gets a bit tricky in this System Tray situation. Since a click of the System Tray shifts the focus to itself, the WinActivate command does not jump back to the proper process—unless you have previously saved the window’s title or id. Plus, clicking the System Tray menu does not give you a programmatic opportunity to capture that information. Remedies can get far too complicated.

Last Window Shortcut Key Combination

Fortunately, Windows offers a shortcut which in many cases immediately resolves the problem without a ton of gymnastics. The key combination Alt+Esc (in AutoHotkey !{Esc}) jumps to the previous on-top window. I used the following in the MousePrecise.ahk script:

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

The AddClass subroutine first jumps back to the original active window (!{Esc}), then runs the Hotkey combination Label subroutine ^SC053. This works like a charm and avoids many convoluted approaches for finding the last (on-top) window. (This technique also works for pop-up GUI windows that don’t close upon execution.)

Note: This “go-to-last-window” shortcut does not see always-on-top windows since the operating system does not include them in the ordered window stack.

But what if the window you want active does not sit on top of the Windows process stack. You need another approach for keeping track of your target.

Tracking a Process Window with AutoHotkey

For a more robust and reliable window seeking method I recommend the window tagging technique found in Chapter Fifteen of Beginning AutoHotkey Hotstrings. In that chapter, I demonstrate the use of GUI commands to insert dates and times into editing fields.

Rather than depending upon the editing window as the last window accessed, I switched to saving the original window’s Unique ID—later using that ID to reactivate the window before inserting the selected date. This technique works exceptionally well. Plus, it has the added benefit of allowing me to access an unlimited number of other windows before I make the final time/date selection. The script always adds that menu date to the appropriate editing field. Use the following two-step fix:

  1. Capture the Unique ID of the original editing window using the WinActive() function command:
    ActWin := WinActive("A")

    Note: The AutoHotkey command WinGet, ActWin, ID, A also works for capturing the Unique ID  and tracking the original active window.

  2. Later, reactivate that same window using the WinActivate command and that same code Unique ID just before inserting the new replacement text (or performing any other AutoHotkey operations):
    WinActivate, ahk_id %ActWin% ;activate original window

    This ensures the activation of the same process window.

The lack of a programmatic opportunity to grab the window’s Unique ID acts as the primary obstacle to this type of window tagging. If using a Hotkey to initiate action, you can simply add the code at a point before leaving the active process. Not so for pop-ups such as the System Tray icon right-click menus.

Tag and Recall Any Process Window

To ensure absolute tracking of a particular process window, create a Hotkey combination which captures the Unique ID for later use within the script for either activating the window or use with the ControlSend or Control command:

!#t::
  WinTag := WinActive("A")
Return

Then to return to the window at any time with:

!#r::
  WinActivate, ahk_id %WinTag%
Return

Put these two Hotkeys in any script to tag and later recall any window. You can use the Unique ID (i.e. ahk_id %WinTag%) in any conforming AutoHotkey command in place of the WinTitle parameter.

If finding a particular window presents a problem for one of your scripts, then either the Alt+Esc shortcut or the window tagging technique may present a solution.

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

Find my AutoHotkey books at ComputorEdge E-Books!

One thought on “AutoHotkey Tip of the Week: Tricks for Tracking and Activating Target Process Windows

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 )

Facebook photo

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

Connecting to %s