AutoHotkey Tip of the Week: Use ToolTips to Make Apps User-Friendly—August 26, 2019

Tip: By Applying the OnMessage() Function, You Can Include Windows ToolTip Notes in Your AutoHotkey GUI Window Scripts to Add Hints and/or Instructions Which Pop-up When Mousing-Over the Controls

Light Bulb!People might find some cleverly-designed AutoHotkey scripts confusing. Wouldn’t you like to add hints and tips which explain each control in your AutoHotkey GUI window? You can do just that with the ToolTip command and the OnMessage() function.

coverepub-250In the book AutoHotkey Applications, “Chapter Thirty-four: Pop-up Labels for All Your Programs (ToolTip Command)”, the Tooltip command highlights each control in an AutoHotkey GUI (Graphical User Interface) window. Use the ToolTip command to add helpful little informational notes to your AutoHotkey applications. Once you implement this tip, whenever you hover the mouse cursor over controls in an AutoHotkey GUI, the ToolTip command will identify the control’s function or give instant instructions (as shown below).

(Extra Tip: You can use this same technique with other non-AutoHotkey Windows application by identifying window and control names with Windows Spy or the WindowProbe.ahk script, then writing a short companion AutoHotkey script to work with the Windows program.)

ToolTipHelp
An AutoHotkey GUI shows three different ToolTips as the cursor is moved over each control.

In this example, the AutoHotkey OnMessage() function monitors a built-in Windows Message (WM_MOUSEMOVE = 0x200; see the send message list) for detecting mouse movement, then uses the called function CheckControl to activate the ToolTip. With this approach, you don’t need other AutoHotkey commands such as SetTimer to set the ToolTip.

First, you set up a GUI. This example does this in the first three lines of code using the Gui, Add command:

Gui, Add, Button, , Hover Here!
Gui, Add, Button, Default, Click Here!
Gui, Add, Edit, w300 h200 ys, Edit text in this box.
Gui, Show, , ToolTip Demo

The OnMessage() function act as the primary mover in this script. OnMessage() employs Windows System Messages to monitor or enact various Windows functions:

OnMessage(0x200, "CheckControl")

In this case, the message 0x200 (WM_MOUSEMOVE) monitors the movement and position of the mouse. Setting up the GUI and the OnMessage() function complete the main script.  Any movement of the mouse calls the function CheckControl():

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
    .`nYou can use multiple lines." ; Line continuation

  ToolTip % Message
}

Library Benefits

The script does its work with this CheckControl() function. The MouseGetPos command (MouseGetPos,,,, VarControl) provides the necessary data to complete the action—in this case, the name of the GUI control under the mouse cursor (VarControl).

The series of IF conditionals found in the CheckControl() function use the IfEqual command—a variation of the standard IF called the command-name style. The use of these types of conditionals can get confusing. They are designed to work with one other command on the same line or other operations (or a command) on the next line only. You cannot use containing curly braces to add more lines.

Note: Although these command-name style IF commands continue to work in AutoHotkey Version 1.1, official AutoHotkey documentation considers them deprecated and recommends switching to the more universal If Statement.

Depending upon the identified control name (e.g. Button1, Button2, and Edit1), the script sets the variable Message to the corresponding text in the conditional. The ToolTip then displays that value using the Tooltip command. When you move the mouse cursor over a control, the appropriate ToolTip pops up. It disappears when you move the cursor off the control.

The simplicity of this ToolTip script makes it easy to add pop-up help messages to AutoHotkey GUI windows and many other non-AutoHotkey programs:

Gui, Add, Button, , Hover Here!
Gui, Add, Button, Default, Click Here!
Gui, Add, Edit, w300 h200 ys, Edit text in this box.
Gui, Show, , ToolTip Demo

OnMessage(0x200, "CheckControl")

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
     .`nYou can use multiple lines."  ; Line continuation

ToolTip % Message
}

For more information on this technique and many other helpful AutoHotkey tips and tricks related to AutoHotkey GUIs, see the book AutoHotkey Applications.

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

One thought on “AutoHotkey Tip of the Week: Use ToolTips to Make Apps User-Friendly—August 26, 2019

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