Pressing GUI Buttons with a Single Keystroke (AutoHotkey Tip)

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.

hotkeycover200While 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
Test Buttons
We plan to press each button with keystrokes.

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

Test Button PressedUsing 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.

Since the built-in variable A_ThisLabel contains the name of the currently active Label, I use it with the MsgBox command to identify each clicked button.

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.

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

Advertisements

2 thoughts on “Pressing GUI Buttons with a Single Keystroke (AutoHotkey Tip)

  1. It would help me if you, at the end of these good tips, posted the entire working code. I was able to compose this one and it worked. But other times I’m not experienced enough to be able to take code snippets and combine them into a workable script. Thank you

    Like

  2. Hi Jack
    Thanks for making such useful contributions to AHK.
    I found QuickLinks to be useful but then was looking to make it even more helpful by having key shortcuts.
    I figured out how do do it! You can let others know if you can point out that add an ampersand in front of the letter of the folder or shortcut.
    Thanks

    Like

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 )

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