Tips for Smoothing Out AutoHotkey Scripts with #If, Tooltip, and Quick Release Hotkeys (Beginning Hotkeys Part 19)

Enhancing the MousePrecise.ahk AutoHotkey Script with the #If Directive, the Tooltip Command, and a Quick Release Hotkey

It’s time to wrap up the MousePrecise.ahk script discussion. Before my recent diversions into the problems with the Dropbox download page (now supplemented with the “Free Scripts” page), I was digging through the incremental mouse micro movement script MousePrecise.ahk line by line. As with most scripts, a few features in the MousePrecise script could work smoother. The script needs:

  • A condition to stop blocking the left mouse button in other non-graphic apps when LButton Down toggles on.
  • Tooltip pop-up reminder telling the user that the left mouse button is still down.
  • A quick release Hotkey to terminate LButton Down if things get lost…or crazy.

(New to the MousePrecise.ahk discussion? To get up to speed, you may want to start at the beginning of this discussion.)

ComputorEdge AutoHotkey E-Books*         *          *

This beginning Hotkey blog builds upon the discussions in the previous parts. If you find any of the information too confusing, then reviewing earlier blogs may be worthwhile.

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

*          *          *

Blocking the Left Mouse Button (LButton)

The MousePrecise.ahk script blocks left mouse button action whenever the numeric keyboard zero Hotkey (Numoad0) activates the LButton Down toggle. This mouse button trap prevents unwanted left mouse button action until the toggle releases LButton Down:

#If toggle
   LButton::Return
#If

However, as written, it blocks left mouse button action everywhere. If, after the script renders the left mouse button useless, another non-graphics window inadvertently activates (e.g. ALT+TAB), returning to the original window presents an aggravating problem. Even when you do get back to the target window, the original implementation of the script’s action gets lost. It would be better if the script only blocked the left mouse button action within the graphics apps. Fortunately, unlike the #IfWin directives, the #If directive accepts expressions and multiple conditions making it easy to isolate the blocking of the left mouse button:

#If toggle and WinActive("ahk_group Graphics") ;disable the left mouse button
  LButton::Return
#If

By adding the WinActive(“ahk_group Graphics”) function to the #If directive, the script no longer blocks the left mouse button anywhere outside the Graphics group windows. This example demonstrates the power of the #If directive when evaluating multiple conditions in expressions.

The script only disables LButton when focused on an active window in the Graphics group with the toggle set on. This technique eliminates the loss of left mouse button functionality in non-graphic apps when the active window accidentally changes. However, it does not totally prevent strange behavior when another window inadvertently activates while the LButton Down condition remains in force. If the graphics window loses focus with the LButton Down toggle active, then it’s best to reset and start over. To reset the script either toggle off (Numpad0) when you get back to the original window or use the new quick release Hotkey discussed at the end of this blog.

But first, let’s be sure we know whether the toggle is on or off.

A Tooltip “Left Mouse Button Down” Reminder

When the window loses focus while MousePrecise.ahk holds the left mouse button down, the graphics apps tend to lose their place. Even if you pop back into the window, the last activity no longer reacts—although LButton Down remains in effect. Plus, the left mouse button block continues. Pressing the 0 key on the numeric keypad toggles the left mouse button, but you may not remember the position of the toggle (Up or Down). The MousePrecise.ahk script needs a pop-up reminder visually displaying the position of LButton. The Tooltip command creates a perfect way to display an important non-intrusive message.

By adding an informational window to the script, you no longer need to guess the state of the left mouse button (Up or Down). The beauty of the Tooltip is that it can be added to any screen without affecting anything else. It just sits there until you tell it to go away:

Tooltip, Left Button Down`rNumpadEnter to cancel, 20,20
TooltipMessage
The Tooltip command adds a floating text informational window. In this case, the Tooltip appears in an always-on-top mode whenever the script toggles Lbutton Down.

This Tooltip command adds an information pop-up to the screen at the 20,20 location—relative to the upper left-hand corner of the window. It maintains an always-on-top position on the screen even if the window loses focus. (The `r symbol tells AutoHotkey to start a new text line in the Tooltip pop-up.)

Note: The second line in the Tooltip (NumpadEnter to cancel) highlights the toggle escape Hotkey discussed below.

Add this code line to the initial toggle on routine. Insert a single Tooltip command (no parameters) to the toggle off routine. This single word removes the Tooltip from the screen.

The visual “Left Button Down” warning provides positive feedback—making the script more user-friendly.

While working in a window in the Graphics group after toggle on, pressing Numpad0 toggles the LButton Down back to LButton Up. However, if the window loses focus, the toggle no longer works. What’s needed is an emergency escape Hotkey.

Quick Release Hotkey Terminating LButton Down

Since there are so many ways that a person can get interrupted while using any app (not just the MousePrecise.ahk script), setting up a method for resetting the script only makes sense. That way everything goes back to start—no harm, no foul. For this situation, I decided to set up a Hotkey which would toggle off the LButton Down condition from anywhere. Since we need this Hotkey only while the AutoHotkey script holds down the left mouse button, we will activate the Hotkey with the same toggle.

But first, we must set up the quick escape Hotkey for LButton Down:

NumpadEnter::       ; Escape Hotkey
  Tooltip           ; turn Tooltip off
  toggle = 0        ; set toogle to off
  Send {LButton Up} ; release LButton
Return

Position the above Hotkey snippet (NumpadEnter::) anywhere in the script after the last #If directive. That way the previous #If and #IfWin directives won’t affect the availability of this Hotkey.

This Hotkey serves three functions. First, the single Tooltip command (no parameters) turns off the Tooltip message. Second, the toggle resets to false (toggle = 0). Lastly, the Send command releases the left mouse button (Send {LButton Up}). These three commands effectively clear the previous action. However, since we don’t need the Hotkey in effect until an LButton Down action, we turn it off in the auto-execute section of the script:

Hotkey, NumpadEnter, off

This Hotkey command, when placed in the auto-execute section of the script, executes after the initial Hotkey definition, thus deactivating it. Now, the Hotkey can be turned on or off at will.

By placing an activating Hotkey command in the toggle on section, the script activates the clearing Hotkey whenever executing the LButton Down toggle in a graphics window:

Hotkey, NumpadEnter, on

This procedure makes the escape Hotkey available at the right time while minimizing its impact on other scripts or apps. While the Tooltip pop-up appears on the screen, pressing the NumpadEnter Hotkey cancels any previous LButton Down action.

The final on/off toggle routine appears as follows:

 if (toggle := !toggle)
 {
   Hotkey, NumpadEnter, on
   Send {LButton Down}
   Tooltip, Left Button Down`rNumpadEnter to cancel, 20,20
 }
 else
 { 
   Hotkey, NumpadEnter, off
   Send {LButton Up}
   Tooltip
 }

The quick escape Hotkey turns on, then off, using the toggling Hotkey command (Numpad0); the left mouse button toggles down, then up; and the information Tooltip pop-up turns on, then off. If you plan to add any other features which need toggling, place them in the above conditional.

Adding these three features (limits on left mouse button blocking, a reminder Tooltip, and an escape Hotkey), reduces the possibilities of problems from lost window focus while making the script more user-friendly.

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