AutoHotkey Tip of the Week: ToolTip Command Tricks

See What You Can Do with the AutoHotkey ToolTip Pop-up!

AutoHotkey AutoHotkey Library Deal!

The ToolTip command acts as a conduit for communication between an AutoHotkey script and its user. Similar in function to other visual information tools (the SplashText command, discussed in a previous blog,  and the Progress/SplashImage command used in the InstantHotstring.ahk script), the more compact ToolTip follows the mouse cursor by default, rather than planting itself in the middle of the screen. You can use the tiny pop-up window in several different ways:

  1. Action Complete ToolTip
  2. Toggle Status Tooltip
  3. Reminder ToolTip
  4. Realtime Informational ToolTip
  5. Instructional ToolTip
  6. Multiple ToolTips

Unlike the other information-passing AutoHotkey command, the ToolTip command offers a smaller footprint while allowing multiple instances (WhichToolTip, 1 to 20) on the screen simultaneously:

ToolTip, Text, X, Y, WhichToolTip

The following examples show some of the ways I’ve used the command in various scripts.

Action Complete ToolTip

Action Completed ToolTip for CopyRefTagWin.ahk

I updated the CopyRefTagWin.ahk script in a recent blog to send selected reference information to a targeted editing window.  Since the script does its work behind the scenes, I wanted positive feedback when the copy action occurs. I added a ToolTip command, paused for three seconds, then turned it off.

   ToolTip, Text inserted into %WinTitle% `rControl
          : %control%!
   Sleep, 3000
   ToolTip  ; turns off ToolTip

This simple code saves me from wondering if the Hotkey combination actually activated its routine.

Toggle Status Tooltip

Toggle Status ToolTip

While I introduced the window always-on-top toggle in Chapter Ten: “Make Any Window Always-on-Top Anywhere, Anytime, Plus More ‘If’ Statements” of A Beginner’s Guide to AutoHotkey, I tended to forget the current “on-top” mode for any given window. I needed a pop-up to tell me the state after I toggled the mode:

  WinSet, AlwaysOnTop, toggle, A
  WinGet, ExStyle, ExStyle, A
  Tooltip,  % (ExStyle & 0x8 = 0) ? "Always-On-Top OFF" 
            : "Always-On-Top ON"
  Sleep, 1500
  ToolTip

The subroutine checks the ExStyle of the active window, then pop-ups a toggling ToolTip message using a forced expression to display the window’s current “on-top” state. I discuss the above snippet in Chapter 5.1.5 “Examine Window Status with WinGet, ExStyle” of Jack’s Motley Assortment of AutoHotkey Tips.

Reminder ToolTip

Left mouse down key state reminder

Sometimes, when working with the MousePrecise.ahk script, I forget that I’ve activated the “hold down the left mouse button” Hotkey. That occasionally causes me a little confusion. In Chapter Nineteen “Tips for Smoothing Out AutoHotkey Scripts with #If, Tooltip, and Quick Release Hotkeys” of the book AutoHotkey Hotkey Techniques, I discuss how to add a ToolTip as a continual reminder that the user has pressed the Hotkey for programmatically holding down the left mouse button.

This time the pop-up message to sits in the upper left-hand corner (20,20) rather than annoyingly following the mouse cursor around. The first line turns on the ToolTip:

Tooltip, Left Button Down`rNumpadEnter to cancel, 20,20

When deactivating the left-mouse-button-down state, the second ToolTip command turns off the pop-up message:

Tooltip           ; turn Tooltip off

Notice that, in this case, the routine does not timeout the message.

Realtime Informational ToolTip

Realtime Informational ToolTip

By adding a recurring SetTimer command, an AutoHotkey script can create an updating Tooltip. In the example at the right, as the mouse cursor moves over an open window, the WindowProbe.ahk script (similar to Window Spy) provides instant readings of window and control properties and names. Introduced in Chapter Thirty-four “Pop-up Labels for All Your Programs (ToolTip Command)” of AutoHotkey Applications: Ideas and Tips for Writing Practical AutoHotkey Scripts, the script checks the window under the mouse cursor every half second:

Persistent
SetTimer, WatchCursor, 500
Return

WatchCursor:
  MouseGetPos, , , id, control
  WinGetTitle, title, ahk_id %id%
  WinGetClass, class, ahk_id %id%
  ControlGetFocus, ActiveWin, A
  ToolTip, Unique ID: ahk_id %id%`nTitle
         : %title%`nClass
         : ahk_class %class%`nControl
         : %control%`nActive Control
         : %ActiveWin%`n`nCTRL+WIN+T to Toggle On 
           and Off
           or `nCTRL+F12 to Copy to Clipboard
Return

^#T:: 
  SetTimer, WatchCursor, % (Toggle:=!Toggle) 
                         ? "Off" : "On"
  If Toggle
    ToolTip
Return

^F12::ClipBoard := "Unique ID: ahk_id " . id 
                . "`r" . "Title: " . title 
                . "`r" . "Class: ahk_class" .  class 
                . "`r" . "Control: " 
                . control . "`r" 
                . "Active Control: " . ActiveWin

(The above snippet uses line continuation techniques to wrap lines of code for display purposes.)

CTRL+WIN+T toggles the probe on and off. CTRL+F12 saves data to the Windows Clipboard.

Instructional ToolTip

Similar to the SetTimer update method used above, this instructional ToolTip technique uses the OnMessage() function to continually monitor the mouse movement (WM_MOUSEMOVE := 0x200).

Instructional ToolTips return messages based upon the control underneath the mouse cursor.
Instructional ToolTips return messages based upon the control underneath the mouse cursor.

Discussed in Chapter Thirty-four, “Pop-up Labels for All Your Programs (ToolTip Command): How to add ToolTip labels that magically appear when hovering” of AutoHotkey Applications: Ideas and Tips for Writing Practical AutoHotkey Scripts, the script continually returns the GUI control name beneath the mouse cursor and posts the appropriate message:

OnMessage(0x200,"CheckControl") ; WM_MOUSEMOVE = 0x200
Return

CheckControl()
{
  MouseGetPos,,,, VarControl
  IfEqual, VarControl, Button1
    Message := "You're hovering!"
  Else IfEqual, VarControl, Button2
    Message := "Click me!"
  Else IfEqual, VarControl, Edit1
    Message := "Enter text in this edit field
             .`nThere can be multiple lines."
  ToolTip % Message
}

This trick enables a mouse hovering over various controls in a GUI pop-up window to produces excellent ToolTip help pop-ups.

Multiple ToolTips

While I have yet to use the following technique in one of my scripts, I can see its potential. For example, a script could set up a series of status or milestone tooltips which toggle or disappear upon status change or task completion, respectively.

The ToolTip command hovers multiple information pop-ups over your window.

The following example script places three tooltips on the window, waits five seconds, then deletes each in order:

ToolTip ,ToolTip 1, 200,120, 1
ToolTip ,ToolTip 2, 230,140, 2
ToolTip ,ToolTip 3, 260,160, 3
Sleep 5000
ToolTip ,,,, 1
ToolTip ,,,, 2
ToolTip ,,,, 3

When used either on its own or in conjunction with other AutoHotkey commands, the ToolTip command provides an excellent tool for communicating script action and status to the user.

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!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!

One thought on “AutoHotkey Tip of the Week: ToolTip Command Tricks

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