user14430182
user14430182

Reputation: 75

Why does my AutoHotKey script not send the checkbox when pressing Enter/NumpadEnter -- only when clicking the button?

I have this code:

#Persistent
#SingleInstance Off

Gui, New, -MinimizeBox, % "Test"
Gui, Add, Edit, vUserInput w300
Gui, Add, Checkbox, vMyCheckbox1, % "Ceckbox one"
Gui, Add, Checkbox, vMyCheckbox2, % "Checkbox two"
Gui, Add, Button, gOkButtonPress w50 h25 x80, % "OK"
Gui, Show, Center
return

Enter::
Gosub, OkButtonPress

NumpadEnter::
Gosub, OkButtonPress

OkButtonPress:
    Gui, Submit
    MsgBox, Meow! %MyCheckbox2%
    ExitApp
return

If I run it and click the "OK" button, it pops up a GUI box saying "Meow! 0" or "Meow! 1" depending on if the second checkbox is checked or unchecked. This is how I want it to be.

However, if I press Enter or NumpadEnter, it only goes "Meow!" with no "0" or "1"; the checkbox value is ignored.

How is this possible? I've cursed over this for several hours yesterday and today, helplessly trying to read the manual and searching but finding nothing.

I find AutoHotKey to be very strange in general.

(I've also tried adding return after each of those Gosubs, and many other things.)

Upvotes: 0

Views: 659

Answers (1)

0x464e
0x464e

Reputation: 6489

This is a very weird thing indeed.
I'd say it's my bad for using Gui, New in the other answer I wrote for you.

Gui, New creates a new gui, and sets it as the default gui, just like any other method of creating a gui would. But it only sets it as the default gui for the current thread.
So, when you run a hotkey, you're in a hotkey thread.
And in that thread it's not the default gui, instead a gui named 1 is the default gui.
So the call to Gui, Submit tries to refer to the gui 1, but it doesn't exist.

You'd fix this by naming your gui, and then referring to the gui by its name:

Gui, MyCoolGui:New, -MinimizeBox, % "Test" ;name the gui
Gui, Add, Edit, vUserInput w300
Gui, Add, Checkbox, vMyCheckbox1, % "Ceckbox one"
Gui, Add, Checkbox, vMyCheckbox2, % "Checkbox two"
Gui, Add, Button, gOkButtonPress w50 h25 x80, % "OK"
Gui, Show, Center
;each of the gui commands couldve been explicitly specified
;to operate on the named gui "MyCoolGui" like MyCoolGui:Add, MyCoolGui:Show
;but it's redundant
return

Enter::         ;no need to gosub, or goto, 
NumpadEnter::   ;just stack the hotkeys and labels
OkButtonPress:  ;like this
    Gui, MyCoolGui:Submit   ;refer to our named gui
    MsgBox, % "Meow! " MyCheckbox2
    ExitApp
return

Alternatively you could not use Gui, New, and you could do this:

Gui, -MinimizeBox
Gui, Add, Edit, vUserInput w300
Gui, Add, Checkbox, vMyCheckbox1, % "Ceckbox one"
Gui, Add, Checkbox, vMyCheckbox2, % "Checkbox two"
Gui, Add, Button, gOkButtonPress w50 h25 x80, % "OK"
Gui, Show, Center, % "Test" ;give a title on the show command
return

Enter::         
NumpadEnter::   
OkButtonPress:  
    Gui, Submit 
    MsgBox, % "Meow! " MyCheckbox2
    ExitApp
return

The documentation for Gui, New describes some of the ups and downs for for using either of these two methods.
But if you're just sticking to one single gui, instead of having multiple ones, maybe I would for simplicity's sake recommend not using Gui, New so you won't have to worry about naming your gui.

Upvotes: 2

Related Questions