Trying AHK2 for the first time Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Trying AHK2 for the first time

Post by boiler » 24 Mar 2023, 07:15

The reason your original version didn't work, is you were tying the event to the GUI object, not the button control object. You can make the button its own object if you want:

Code: Select all

#Requires AutoHotkey v2.0
AHKV2Gui := Gui("AlwaysOnTop", "my title")
BtnCtrl := AHKV2Gui.Add("Button", "vMyButton", "ButtonText")
BtnCtrl.OnEvent("Click", AHKV2Gui_Click)
AHKV2Gui.Show("w400 h300")
return

AHKV2Gui_Click(*) {
	AHKV2Gui.Hide()
    MsgBox("blabla")
	AHKV2Gui.Show()
}

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 24 Mar 2023, 08:18

Ah I see. Clear. Thanks again. Eventually I hope to get the hang of it. It's quite different from V1.

User avatar
RaptorX
Posts: 378
Joined: 06 Dec 2014, 14:27
Contact:

Re: Trying AHK2 for the first time

Post by RaptorX » 25 Mar 2023, 08:01

The Events functions must be defined based on the parameters that the event send (some events have different parameters than others.

The Click Event receives 2 parameters obj and info so your function must look like this:

Code: Select all

ClickTheButton(obj, info)
{
    ; code
}
But if you dont care about the parameters (as it probably is your case) then you use * to indicate that you can ignore them... thats the reason you can use:

Code: Select all

ClickTheButton(*)
{
    ; code
}
Hope that helps understand why the first code you sent didnt work besides of adding the event to the GUI object instead of the control object. :)
Projects:
AHK-ToolKit

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 27 Mar 2023, 02:14

@RaptorX Thanks for the explanation !

Could you (or anyone else) perhaps help me out with the correct syntax to do the following ?
I found repeating the button over and over again a little reduntant so I tried doing it using a loop

like so, unfortunately it doesn't work:

Code: Select all

Loop 10
    {
        TableButton.A_Index := AHKV2Gui.Add("Button", "w40", A_Index)
    }

; THIS WORKS:
/*
TableButton1  := AHKV2Gui.Add("Button","w40", "1")
TableButton2  := AHKV2Gui.Add("Button","w40 yp", "2")
TableButton3  := AHKV2Gui.Add("Button","w40 yp", "3")
TableButton4  := AHKV2Gui.Add("Button","w40 yp", "4")
TableButton5  := AHKV2Gui.Add("Button","w40 yp", "5")
TableButton6  := AHKV2Gui.Add("Button","w40 yp", "6")
TableButton7  := AHKV2Gui.Add("Button","w40 yp", "7")
TableButton8  := AHKV2Gui.Add("Button","w40 yp", "8")
TableButton9  := AHKV2Gui.Add("Button","w40 yp", "9")
TableButton10 := AHKV2Gui.Add("Button","w40 yp", "10")
*/

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Trying AHK2 for the first time

Post by boiler » 27 Mar 2023, 02:57

TableButton.A_Index is never correct in v1 or v2 for using a variable to reference a key. It would be TableButton[A_Index]. In v2, use .Push() to assign elements with a new index. And you must initialize the array.

Code: Select all

TableButton := []
Loop 10
	TableButton.Push(AHKV2Gui.Add("Button", "w40", A_Index))

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 27 Mar 2023, 03:08

Ah I see thanks. I didn't have V1 completely down yet as well so sorry for my mistake.

I have to say I really like the AHK community. They are always very helpfull and not as toxic as for example
some Linux form users who expect you to know everything and only say RTFM !
Last edited by WalkerOfTheDay on 27 Mar 2023, 03:24, edited 1 time in total.

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Trying AHK2 for the first time

Post by boiler » 27 Mar 2023, 03:15

Although the manual should always be referenced first, concepts can be unclear, especially in making the transition from v1 to v2. Objects are treated differently between the two, for example, and it would be understandable to try to use the dot notation (though not with a variable even in v1).

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 27 Mar 2023, 07:10

boiler wrote:
27 Mar 2023, 02:57
TableButton.A_Index is never correct in v1 or v2 for using a variable to reference a key. It would be TableButton[A_Index]. In v2, use .Push() to assign elements with a new index. And you must initialize the array.

Code: Select all

TableButton := []
Loop 10
	TableButton.Push(AHKV2Gui.Add("Button", "w40", A_Index))
In this case, how do I create the click event for each button ??

Edit

Probably something like this right ?

Code: Select all

Loop 10
    TableButton[A_Index].OnEvent("Click", TableButton1_Click)
 

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Trying AHK2 for the first time

Post by boiler » 27 Mar 2023, 07:48

The best way is to find out is to try it:

Code: Select all

TableButton := []
AHKV2Gui := Gui()
Loop 10 {
	TableButton.Push(AHKV2Gui.Add("Button", "w40", A_Index))
    TableButton[A_Index].OnEvent("Click", TableButton1_Click)
}
AHKV2Gui.Show()
return

TableButton1_Click(*) {
	MsgBox "hello"
}

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 27 Mar 2023, 08:18

Yes that works. Sorry for asking another question, but how would I be able to put in a MsgBox, which button was pressed ?
I tried:

Code: Select all

TableButton1_Click(*) {
            MsgBox TableButton1.Text
}

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Trying AHK2 for the first time

Post by boiler » 27 Mar 2023, 09:16

No problem asking questions. These things aren't always obvious. Yes, that can be done. The first parameter is the object that launched the event, so that can be accomplished like this:

Code: Select all

#Requires AutoHotkey v2.0

AHKV2Gui := Gui()
TableButton := []
Loop 10 {
	TableButton.Push(AHKV2Gui.Add("Button", "w40", A_Index))
    TableButton[A_Index].OnEvent("Click", TableButton1_Click)
}
AHKV2Gui.Show()
return

TableButton1_Click(btnObj, *) {
	MsgBox btnObj.Text
}

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 27 Mar 2023, 10:08

Very nice ! Thank you. I'm learning a lot.

So now I'd like to respond to a value entered into an edit control, how would I go about that.
This is my unfinished code:

Code: Select all

#Requires AutoHotkey v2.0

counter := 1
mytable := 1

AHKV2Gui := Gui("AlwaysOnTop", "My First AHKv2 Gui")
Ahkv2Gui.Add("Text",, "Which table do you want to practice ?")
Ahkv2Gui.Add("Text",, "")

TableButton := []
Loop 10
    {
        TableButton.Push(AHKV2Gui.Add("Button", "w40 yp", A_Index))
        TableButton[A_Index].OnEvent("Click", TableButton1_Click)
    }
	
AddCtrl     := Ahkv2Gui.Add("Text", "x10 y+30 hidden", counter . " times " . mytable " is ?")
EditCtrl    := Ahkv2Gui.AddEdit("w40 yp-5 xp+70 hidden")
AHKV2Gui.Show("w500 h300")
return

TableButton1_Click(btnObj, *) {
    mytable := BtnObj.Text
    Loop 10
        {
            TableButton[A_Index].Enabled := false ; disable all buttons until finished
        }
    AddCtrl.Text := counter " times " mytable " is: "
    AddCtrl.Visible     := 1
    EditCtrl.Visible    := 1
    if EditCtrl.Value = counter * mytable
        MsgBox "Correct"
    else
        MsgBox "Incorrect, please try again"
    return
}


User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Trying AHK2 for the first time

Post by boiler » 27 Mar 2023, 11:08

You're launching all the code including your checking of the entry before there has been a chance for the user to enter anything. You have to think through which events are going to launch which functions. You want the button only to launch the code that displays the entry edit box, then you'll need some other event, like a button you haven't added yet that the user would press after they've typed their entry to submit it. Then when that event occurs, you would need that other function to get what the user entered from that edit box as is shown in the GUI example #2.

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 27 Mar 2023, 13:15

Okay I will have a look at it tomorrow when I have a fresh mindset :lol: (hopefully)

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 28 Mar 2023, 04:41

Okay, with the gui example you linked to I think I'm getting somewhere I'll post what I have later on.

In the meantime I have another question.

Code: Select all

AHKv2Gui.Add("Button", "yp hidden" , "Go").OnEvent("Click", ClickAnswer)
Is it possible to unhide above button without doing something like this:

Code: Select all

AnswerBtn   :=AHKv2Gui.Add("Button", "yp hidden" , "Go").OnEvent("Click", ClickAnswer)

Code: Select all

AnswerBtn.Visible := 1

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Trying AHK2 for the first time

Post by boiler » 28 Mar 2023, 04:56

It may be possible via other ways of identifying the GuiControl object like via its hwnd or enumerating through the GUI's controls and stopping on the desired one, which you would have had to ascertain, but I can't see that anything would be more straightforward than what you are trying to avoid.

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 28 Mar 2023, 08:24

Okay thanks, then I'll use the second method :thumbup:

Here's what I came up with so far. I probably did some strange coding, but it seems to be working.

The only thing that took me quite some time to find out is that in the ResetGui function I needed to
make the variables counter and mytable global, otherwise they wouldn't get reset to 1.

Code: Select all

#Requires AutoHotkey v2.0
counter := 1
mytable := 1

AHKV2Gui := Gui(, "My First AHKv2 Gui")
Ahkv2Gui.Add("Text",, "Which table do you want to practice ?")
Ahkv2Gui.Add("Text",, "")
AHKv2Gui.Show()

TableButton := []
Loop 10
    {
        TableButton.Push(AHKV2Gui.Add("Button", "w40 yp", A_Index))
        TableButton[A_Index].OnEvent("Click", TableButton1_Click)
    }
AddCtrl             := Ahkv2Gui.Add("Text", "x10 y+30 hidden", counter . " times " . mytable " is ?")
EditCtrl            := Ahkv2Gui.AddEdit("vAnswer center Number w40 yp-5 xp+70 hidden")
AnswerBtn           := AHKv2Gui.Add("Button", "default yp hidden" , "Go")
AnswerBtn.OnEvent("Click", ClickAnswer)
AnswerText          := AHKV2Gui.Add("Text", "x+3 yp+5 hidden", "correct !")
AHKV2Gui.Show("w500 h300")
return

TableButton1_Click(btnObj, *) {             ; btnObj is used to get the text from the pressed button
    global mytable := btnObj.Text
    Loop 10
        {
            TableButton[A_Index].Enabled := false ; disable all buttons until finished
        }
    counter             := 1
    AddCtrl.Text        := counter " times " mytable " is: "
    AddCtrl.Visible     := 1
    EditCtrl.Visible    := 1
    EditCtrl.Focus
    AnswerBtn.Visible   := 1
    return
}

ClickAnswer(*) {
    Saved := AHKV2Gui.Submit(0)
    if Saved.Answer = counter * mytable     ; means the answer is correct
        {
            global counter
            if (counter = 10)
                {
                    ResetGui()       
                    return
                }
                
            ++counter               ; used to be counter++ in V1
            AddCtrl.Text        := counter " times " mytable " is: "
            EditCtrl.Text       := ""
            EditCtrl.Focus
            AnswerText.Text     := "correct"
            AnswerText.SetFont("cGreen")
            AnswerText.visible  := 1     
            AnswerBtn.Enabled   := false
            Sleep 750
            AnswerBtn.Enabled   := true
            EditCtrl.Focus
            AnswerText.visible  := 0     
        }
    else
        {
            AnswerText.Move(,,150)
            AnswerText.Text         := "Your answer is incorrect"
            AnswerText.SetFont("cRed")
            AnswerText.visible      := 1     
            AnswerBtn.Enabled       := false
            Sleep 750
            AnswerText.visible      := 0    
            AnswerBtn.Enabled       := true
            EditCtrl.Focus
            AnswerText.visible      := 0     
        }
    return
}

ResetGui(*)
{
    Loop 10
        TableButton[A_Index].Enabled := true ; enable all buttons
    global counter      := 1
    global mytable      := 1
    AnswerBtn.Visible   := 0
    AddCtrl.visible     := 0
    AnswerText.visible  := 0
    EditCtrl.visible    := 0
    EditCtrl.Text       := ""
    return
}

User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Trying AHK2 for the first time

Post by WalkerOfTheDay » 29 Mar 2023, 07:17

Not sure if I should keep it in this topic or open a new one, but that would probably cause me to create a lot of new topics :)

When I use below code, sometimes I press enter accidentally causing the Go button to be pressed which registers a mistake.
How can I prevent this ?

I was think I would need to do something with EditCtrl.Text, but I'm not sure what ?

edit
Think I solved it (like so:)

Code: Select all

if Saved.Answer = ""
        {
            return
        }
/edit

Code: Select all

if EditCtrl.Text := ""
   don't count the mistake

Code: Select all

#Requires AutoHotkey v2.0
counter     := 1
mytable     := 1
mistakes    := 0
AHKV2Gui := Gui(, "My First AHKv2 Gui")
Ahkv2Gui.Add("Text",, "Which table do you want to practice ?")
Ahkv2Gui.Add("Text",, "")
AHKv2Gui.Show()

TableButton := []
Loop 10
    {
        TableButton.Push(AHKV2Gui.Add("Button", "w40 yp", A_Index))
        TableButton[A_Index].OnEvent("Click", TableButton1_Click)
    }
AddCtrl             := Ahkv2Gui.Add("Text", "x10 y+30 hidden", counter . " times " . mytable " is ?")
EditCtrl            := Ahkv2Gui.AddEdit("vAnswer center Number w40 yp-5 xp+70 hidden")
AnswerBtn           := AHKv2Gui.Add("Button", "default yp hidden" , "Go")
AnswerBtn.OnEvent("Click", ClickAnswer)
AnswerText          := AHKV2Gui.Add("Text", "x+3 yp+5 w150 hidden", "correct !")
NoOfMistakes        := AHKV2Gui.Add("Text", "y65 x328 h25  w250 hidden", "You made " . mistakes . " mistake")
NoOfMistakes.SetFont("s12 cff008c")
AHKV2Gui.Show("w500 h300")
return

TableButton1_Click(btnObj, *) {             ; btnObj is used to get the text from the pressed button
    global mytable := btnObj.Text
    Loop 10
        {
            TableButton[A_Index].Enabled := false ; disable all buttons until finished
        }
    counter             := 1
    AddCtrl.Text        := counter " times " mytable " is: "
    AddCtrl.Visible     := 1
    EditCtrl.Visible    := 1
    EditCtrl.Focus
    AnswerBtn.Visible   := 1
    return
}

ClickAnswer(*) {
    global mistakes
    Saved := AHKV2Gui.Submit(0)
    if Saved.Answer = counter * mytable ; means the answer is correct
        {
            global counter
            if (counter = 10)
                {
                    ResetGui()       
                    return
                }
                
            ++counter               ; used to be counter++ in V1
            AddCtrl.Text        := counter " times " mytable " is: "
            EditCtrl.Text       := ""
            EditCtrl.Focus
            AnswerText.Text     := "correct"
            AnswerText.SetFont("cGreen")
            AnswerText.visible  := 1     
            AnswerBtn.Enabled   := false
            Sleep 750
            AnswerBtn.Enabled   := true
            EditCtrl.Focus
            AnswerText.visible  := 0     
         }
    else
        {
            ++mistakes
            NoOfMistakes.Visible    := 1
            if (Mistakes = 1)
                NoOfMistakes.Text   := "You made " . mistakes . " mistake"
            else
                NoOfMistakes.Text   := "You made " . mistakes . " mistake(s)"
            AnswerText.Text         := "Your answer is incorrect"
            AnswerText.SetFont("cRed")
            AnswerText.visible      := 1     
            AnswerBtn.Enabled       := false
            Sleep 750
            AnswerText.visible      := 0    
            AnswerBtn.Enabled       := true
            EditCtrl.Text           := ""
            EditCtrl.Focus
            AnswerText.visible      := 0     
        }
    return
}

ResetGui(*)
{
    Loop 10
        TableButton[A_Index].Enabled := true ; enable all buttons
    global counter      := 1
    global mytable      := 1
    global mistakes     := 0
    AnswerBtn.Visible   := 0
    AddCtrl.visible     := 0
    AnswerText.visible  := 0
    EditCtrl.visible    := 0
    EditCtrl.Text       := ""
    return
}

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Trying AHK2 for the first time

Post by boiler » 29 Mar 2023, 09:40

WalkerOfTheDay wrote: Not sure if I should keep it in this topic or open a new one, but that would probably cause me to create a lot of new topics :)
You should generally open a new topic when a solution for your original question has been reached. Your subject title of "Trying AHK2 for the first time" is broader than what your question was and is too broad to define a thread topic (i.e., there shouldn't just be an ongoing thread about helping you learn various aspects of v2).

Post Reply

Return to “Ask for Help (v2)”