Reset Hotkeys with Label Name Drop-Through Behavior (AutoHotkey Tip)

Sometimes Not Encapsulating Hotkeys with the Return Command Serves a Purpose

Last time, I discussed how to change the transparency level of any window under the mouse cursor with a scroll of the mouse wheel. The SeeThruWinWheel.ahk works great, but, if you increase the invisibility of the window too much, you might lose track of the window. I needed a method for instantly bringing a faded window back into view. I did that with a trick from the blog “Understanding Label Names and Subroutines (Beginning AutoHotkey Tip).”

AutoHotkey Library Deal
AutoHotkey Library Deal

While studying the behavior of Label names in AutoHotkey scripts, I came up with the CheeseBurgerRecipe.ahk script which automatically moves to the next Hotkey recipe step with no additional code by dropping pass the next Label name directly into its subroutine. I didn’t expect to find another use for this technique so soon, but when I encountered the problem of losing track of invisible windows, this technique offered a quick fix.

Resetting Parameters by Creating a Drop-through Hotkey

I created an instant reset Hotkey (CTRL+LButton) with only three lines of code:

Control & LButton::
  MouseGetPos,,, currentWindow
  %currentWindow% := 255

Notice that the Hotkey deliberately omits the Return command at the end of the subroutine. That allows AutoHotkey to intentionally drop pass the next Hotkey Label thus running its subroutine:

Control & LButton::
  MouseGetPos,,, currentWindow
  %currentWindow% := 255

Control & Wheelup::
  MouseGetPos,,, currentWindow
  if not (%currentWindow%)
  {
    GoSub GetTransparent
  }
  if (%currentWindow% < 255)
  {
    %currentWindow% += 5
  }
  WinSet, Transparent, % %currentWindow%, ahk_id %currentWindow%
  SplashImage,,w150 x0 y0 b fs12, % currentWindow . "—" . %currentWindow%
  SetTimer, TurnOffSI, 1000, On
Return

On its own, the second Hotkey (CTRL+WheelUP) increases the visibility of any window under the hovering mouse cursor. This works for bringing a transparent window back into view, however, it takes some amount of mouse wheel scrolling.

By adding the new Hotkey (CTRL+LButton), the combination of holding down the Control key while clicking the left-mouse button resets the window to full visibility by first capturing the window ID, assigning 255 (max opaqueness) to the transparency variable, then running the next sequential Hotkey in the script setting the window to the new non-transparent level.

This approach to resetting the transparency level takes advantage of the code in the second Hotkey without calling it in the script. On the downside, the drop-through method requires that the two Hotkeys appear sequentially in the script. If new code later intervenes, behavior becomes unpredictable.

You might prefer to use the GoSub command and include the terminating Return command. With this approach, the location of the Hotkey code in the script does not matter:

Control & LButton::
  MouseGetPos,,, currentWindow
  %currentWindow% := 255
  GoSub Control & Wheelup
Return

You could also use the GoTo command with or without the Return command since the resetting of window transparency does not require reversing back to the same point in the script. But, maybe that would be a bit too sloppy. (See “GoTo Command Versus GoSub Command (AutoHotkey Tip).”)

The GoSub example may be a “best practice”, but I hesitate to rule-out the first drop-through method. You might find situations where adding sequential Hotkeys which automatically drop into the next subroutine create an ideal solution.

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