AutoHotkey Tip of the Week: Toggling Features On and Off

While You’ll Find AutoHotkey Offers Many Methods for Turning Things On and Off, This Technique Makes It Easy

ToDoListINIOver the years I’ve used a number of different approaches to changing the state of buttons, checkmarks, and various other aspects of AutoHotkey app. But, as recommended by numerous sources, I prefer a simple variable reset to the opposite state (on/true/1 or off/false/0). The code comes in shorthand form using the logical-not (!) operator:

Toggle := !Toggle

When used in an expression, this form for the logical-not (!) operator switches off to on and on to off.

Note: You can use any variable name in place of the term Toggle. In fact, if you plan to use more than one toggle, I recommend you assign a variable name indicating what it does (i.e. ToggleToolTip).

I recently used this standard toggling approach for changing conditions in the ShowToolTip subroutine in the ToDoListINI.ahk script. This routine turns ToolTip pop-up help messages on and off with the click of the GUI’s menu bar Help menu item.

Toggling Routines

Originally, I added the ToolTip help messages to make the to-do list app a little more user-friendly. It works well for new users, but, eventually, the messages get annoying. I decided to write a subroutine for switching the messages off—then on again.

This ShowToolTip subroutine toggles the ToolTip help messages while adding (or removing) the checkmark in the GUI’s menu bar Help menu:

If (Toggle := !Toggle) {
  OnMessage(0x200, "CheckControl")
  Menu, HelpMenu, Check, &Toggle Help ToolTips`tCtrl+T
Else {
  OnMessage(0x200, "")
  Menu, HelpMenu, Uncheck, &Toggle Help ToolTips`tCtrl+T

In this example, the AutoHotkey OnMessage() function monitors the built-in Windows Message (WM_MOUSEMOVE = 0x200; see the send message list) for detecting mouse movement, then the CheckControl function activates the ToolTip. Bonus Tip: If you employ this OnMessage() approach, you don’t need other AutoHotkey commands such as SetTimer to monitor the mouse cursor position for setting the ToolTip pop-up (or other AutoHotkey actions).

Setting the Toggle


Although it may seem a little confusing, setting a variable equal to not itself (!Toggle) creates a perfect switch.  However, for the switch to work, you must use the set expression operator (colon equal sign := ), not the expression equals sign comparison operator (a sole equal sign = ). When used in an expression, the two operators (:= and =) yield very different results. The colon equal sign (:=) sets or resets a value while the lone equal sign merely compares two expressions.

Chapter Eighteen “AutoHotkey Toggles and the Ternary Operator” of AutoHotkey Hotkey Techniques, demonstrates the difference between the two operators.

NumpadEnter::MsgBox % (Switch := !Switch) ; reset
   ? "Switch is on!" 
   : "Switch is off!"

In this case, the value of Switch changes each time AutoHotkey evaluates the expression. However, if a new AutoHotkey user mistakenly uses the comparison operator, the value never changes:

NumpadEnter::MsgBox % (Switch = !Switch)  ; compare only
   ? "Switch is on!" 
   : "Switch is off!"

Since the expression only compares Switch to !Switch (not Switch), it always evaluates as false—regardless of the initial value of Switch. Be sure to use the colon equal sign ( := ) operator when setting values (versus comparing values) inside an expression.

Note: On the first use of the Toggle := !Toggle expression, unless previously defined in the script, the variable Toggle initiates as false and sets to true.

Toggling the Left Mouse Button Down and Up

The following Hotkey definition from the AutoHotkey Hotkey Techniques book uses the ternary operator to toggle the position of the left mouse button down and up:

SC052::Send % (LeftMouseToggle := !LeftMouseToggle) 
  ? "{LButton Down}" 
  : "{LButton Up}"

I implemented this Hotkey in the MousePrecise.ahk script. Press the zero key on the numeric keypad (SC052) to hold down the left mouse button while using the numeric keypad arrows to move the mouse cursor one pixel at a time, then press zero again to release the mouse button.

Other Types of Switches

CoverEPUB200The above toggling technique works well for pure on/off switches. However, you’ll find times when you only want to change states just once. For example, in Chapter Twenty-one: How to Make a Pop-up Menu for Programs, Web Sites and Files: Reading files from folders to build a menu structure with AutoHotkey” of the book Digging Deeper into AutoHotkey, I discuss how I designed the QuickLinks.ahk script to execute certain Menu commands only on the first iteration of a loop.

I also used this technique in the original ToDoList.ahk script included in Chapter Nine and Chapter Ten of the AutoHotkey Applications e-book for reading the first line of the saved file which sets window positioning then switches to reading to-do list items. When reading the to-do list in a file reading loop, the script reads the first line of the file:

 If (A_Index=1)

as window positioning option codes.

coverepub-250In Chapter Thirty-five, “A Script to Change the Windows Registry (RegRead and RegWrite Command)” of AutoHotkey Applications, I show how to turn Windows Registry items on and off.

Toggling AutoHotkey Commands

In Chapter Eight, “Toggling AutoHotkey Hotkeys On and Off” of AutoHotkey Hotkey Techniques, I highlight how certain AutoHotkey commands offer a Toggle parameter—in this case, the Hotkey command.

A few other AutoHotkey commands, such as, the Hotstring() function, the Suspend command, and disabling/enabling Menu items, offer the toggle option.

A Multitude of AutoHotkey Tips and Tricks

In the book A Motley Assortment of AutoHotkey Tips, Chapter 14.1.3 “Unhide Hidden Files with Windows Registry Setting to Find Lost Data” shows how AutoHotkey can toggle hidden files, system files, and file extensions in the Windows Registry via the UnhideFiles.ahk script.

In the same Motley book, Chapter 13.1.6 “Automatically Launch Apps at Windows Startup: How to Toggle Automatic Loading of AutoHotkey Scripts“, I use the EitherMouse.ahk script as an example of how to create a toggling startup sequence by checking for the existence of a Windows shortcut file. If the file exists in the Startup folder, the toggle removes it. If not, the toggle creates a new shortcut.

Toggling techniques work great for binary choices. But, what if you need to choose from a number of options. Next time, I discuss a relatively new AutoHotkey command for picking from a number of alternatives then executing actions.

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


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



Leave a Reply

Fill in your details below or click an icon to log in: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s