How to make the button do things in AHK 2 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
alpha-centauri
Posts: 6
Joined: 30 Jan 2023, 06:23

How to make the button do things in AHK 2

Post by alpha-centauri » 06 Feb 2023, 06:18

Hi, I have recently started using AHK and I understand how to make a GUI but I can't seem to figure out how to trigger a series of commands when the button is pressed.

Code: Select all

::dummy-text:: {
    Sleep 100
    MyGui := Gui(,"My Gui",)
    MyGui.Add("Text", , "Question 1?")
    Q1 := MyGui.Add("Edit")
    MyGui.Add("Text", , "Question 2?")
    Q2 := MyGui.Add("Edit")
    OK := MyGui.Add("Button", "Default w80", "&OK")
    MyGui.Show("w200")
}
Here I want the OK Button to do the following,

Code: Select all

MyGui.Hide()
SendInput "You Selected " Q1.Value " & " Q2.Value " from the GUI."
How do I go about doing that?

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: How to make the button do things in AHK 2

Post by DuckingQuack » 06 Feb 2023, 07:08

So, this page has a lot of the info you’re looking for:
https://www.autohotkey.com/docs/v2/lib/GuiControl.htm

What it lacks, however, is any decent examples of the syntax needed. Which, for you, will be something like:

Code: Select all

MyGui[“OK”].OnEvent(“OKClick”, OKClicked)
OKClicked(){
;STUFF THE CLICK DOES
}
I cannot test this at the moment so there might be some errors remaining.
Edit: thanks mikeyww, I missed the quotes around event name. Added them.
Last edited by DuckingQuack on 06 Feb 2023, 07:24, edited 1 time in total.
Best of Luck,
The Duck

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: How to make the button do things in AHK 2  Topic is solved

Post by mikeyww » 06 Feb 2023, 07:15

Code: Select all

#Requires AutoHotkey v2.0

MyGui := Gui(,"My Gui",)
MyGui.Add("Text", , "Question 1?")
Q1 := MyGui.Add("Edit")
MyGui.Add("Text", , "Question 2?")
Q2 := MyGui.Add("Edit")
OK := MyGui.Add("Button", "Default w80", "&OK")
OK.OnEvent('Click', go)

:X:dummy-text::MyGui.Show("w200")

go(btn, info) {
 MyGui.Hide
 Send "You Selected " Q1.Value " & " Q2.Value " from the GUI."
}

alpha-centauri
Posts: 6
Joined: 30 Jan 2023, 06:23

Re: How to make the button do things in AHK 2

Post by alpha-centauri » 06 Feb 2023, 07:39

@mikeyww
Hi your code worked for me! Can you tell me where I can learn more about why go(btn,info) was needed?
Also what is the difference between :X: and ::?
Either ways thanks a lot.

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: How to make the button do things in AHK 2

Post by mikeyww » 06 Feb 2023, 07:53

See https://www.autohotkey.com/docs/v2/lib/GuiControls.htm#Button.

Figuring out how the events work is a bit tricky, but see https://www.autohotkey.com/docs/v2/lib/GuiOnEvent.htm#Click.

The syntax for the called function is noted.

Code: Select all

functionName(controlObject, info)
You need the parameters even if you do not use them.

For hotstring options, see https://www.autohotkey.com/docs/v2/Hotstrings.htm#Options.
X: Execute. Instead of replacement text, the hotstring accepts a function call or expression to execute.

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: How to make the button do things in AHK 2

Post by mikeyww » 06 Feb 2023, 10:45

GUI Example for AHK v2

Code: Select all

/* GUI example for AHK v2 --------------------------------------------------------------
This script shows an example of a GUI with a button that calls a function via OnEvent.
Concepts:
1. Name the controls that will have events (i.e., assign control objects to variables)
2. Use guiName.OnEvent or controlName.OnEvent to map events to functions
3. Check documentation for the needed function parameters
By mikeyww on 06 February 2023 • For AutoHotkey version 2.0.2
https://www.autohotkey.com/boards/viewtopic.php?p=505978#p505978
----------------------------------------------------------------------------------------
*/
#Requires AutoHotkey v2.0
ahkDoc := "https://www.autohotkey.com/docs/v2/lib/"                ; URL for AHK v2 function index
gui1   := Gui('+AlwaysOnTop', 'Button action (F3 = Show again)'), gui1.SetFont('s10') ; Set up GUI
gui1.AddLink(, '<a href="' ahkDoc 'GuiControls.htm#Button">Button with event</a> | '  ; Add URLs
             . '<a href="' ahkDoc 'GuiOnEvent.htm#Click">Event function</a>')
ed     := gui1.AddEdit('w400')                ; Add an edit control
btn    := gui1.AddButton('wp Default', 'OK')  ; Add a button
btn.OnEvent('Click', activated)               ; Define event for button click
gui1.OnEvent('Escape', escapeGUI)             ; Define event for GUI escape
showGUI                                       ; Show the GUI

F3::showGui                                   ; F3 = Show the GUI

showGUI() {
 ed.Focus                                     ; Focus on the edit control
 gui1.Show('x1000 y150')
}

activated(btn, info) {                        ; Called when button is activated
 ; gui1.Submit(HIDE := False)
 MsgBox ed.Value, 'Your entry', 64
}

escapeGUI(gui) {                              ; Called when user presses ESC on GUI
 gui.Hide
}

User avatar
FanaticGuru
Posts: 1905
Joined: 30 Sep 2013, 22:25

Re: How to make the button do things in AHK 2

Post by FanaticGuru » 06 Feb 2023, 18:51

mikeyww wrote:
06 Feb 2023, 10:45
GUI Example for AHK v2

A slightly more advanced example:

Code: Select all

guiMain := Gui(,"Main Gui"), guiMain.Edit := {}
guiMain.Add("Text","w75 Right", "Stuff:")
guiMain.Edit.Stuff := guiMain.Add("Edit", "x100 yp w400")
guiMain.Add("Text","y+20 x10 w75 Right", "Other Stuff:")
guiMain.Edit.Other := guiMain.Add("Edit", "x100 yp w400")
guiMain.Add("Button", "Default y+20 xm+100 w150", "Confirm").OnEvent("Click", guiMain_Button_Click)
guiMain.Add("Button", "xm+300 yp w150", "Clear").OnEvent("Click", guiMain_Button_Click)

guiMain.Show

Esc::ExitApp

guiMain_Button_Click(Button, Info)
{
	If (Button.Text = "Confirm")
		MsgBox "Stuff: " guiMain.Edit.Stuff.Text "`nOther Stuff: " guiMain.Edit.Other.Text
	else
		for EditName, EditCtrl in guiMain.Edit.OwnProps()
			EditCtrl.Text := ""
}

This is really more about how to organize things for when you get a lot of Gui with lots of controls.

I find it very useful to keep all handles for a particular Gui within the same object that holds the Gui. Also by using an object structure, you can loop easily through controls as shown here when clearing the Edit controls. When you have 20 checkboxes it can really be useful to loop through them.

Also, this shows that every control event does not have to go to a separate function. It can often condense your code considerably to have a fewer number of more generic event handlers.

This is more for others that might google this thread than for @mikeyww. This just adds a few tricks for those that want to take it a little further.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: How to make the button do things in AHK 2

Post by mikeyww » 06 Feb 2023, 19:36

Looks good. Thank you for the additional example & supporting details, FG.

Post Reply

Return to “Ask for Help (v2)”