Sometimes We Find It Easier to Hit a Key Rather Than Click a Button
An AutoHotkey forum user posted a question about using Hotkeys to activate buttons in a GUI window. He wanted to hit the numbers 1, 2, 3, or 4, rather than clicking the individual buttons—but only for the active GUI window.
While I discuss similar isolation of Hotkeys to specific active windows in my Hotkeys book, this blog provided another opportunity to show how to add more functionality to your GUIs without infringing upon other programs or Hotkeys. I attempted to make the script as simple as possible by using a number of AutoHotkey tricks.
Use Automatically Generated Labels (Subroutines)
First, I set up a GUI window with a series of buttons:
Gui, Add, Button, , Test Button One Gui, Add, Button, ym , Test Button Two Gui, Add, Button, ym , Test Button Three Gui, Add, Button, ym , Test Button Four Gui, Show, , Test Buttons Return
I could have added a g-Label subroutine name for each button but AutoHotkey automatically recognizes Labels generated from the button name. By combining the control name Button with the text on the button (with spaces removed), I create the assumed Label name for each:
ButtonTestButtonOne: ButtonTestButtonTwo: ButtonTestButtonThree: ButtonTestButtonFour: MsgBox, % "You pressed " A_ThisLabel Return
Using another AutoHotkey trick, I stack the Label names so that they all run the same code. Click any one of the buttons and AutoHotkey drops-through the subsequent Label names to run the code after the last Label. Clicking any button pops up the message box.
Note: The percent operator (%) found in the MsgBox command forces the evaluation of the expression following it.
Isolate Button Pressing Hotkeys
By far, the ControlClick command offers the easiest way to press buttons with keyboard keys. You don’t even need to know the names of each button since AutoHotkey numbers the controls in order while placing them in the GUI (e.g. Button1, Button2, Button3, … ).
By adding the #IfWinActive directive to toggle the Hotkeys on only for an active Test Button window, I can use the number keys to press each button:
#IFWinActive Test Buttons 1::ControlClick, Button1 2::ControlClick, Button2 3::ControlClick, Button3 4::ControlClick, Button4 #IfWinActive
Without placing these Hotkeys inside the #IfWinActive directives, the numbers 1-4 would not work properly in other programs.
I could reduce the code to:
#IFWinActive Test Buttons 1:: 2:: 3:: 4::ControlClick, % "Button" A_ThisHotkey #IfWinActive
but I saw the first method as easier to understand without adding too much more code. Note the use of the percent operator (%) to once again force the evaluation of an expression.
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.)