Default button not acting as default

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Default button not acting as default

20 Jun 2021, 04:14

I don't understand what's happening.
I specify the "Default" option but only a thicker border appears over it. The actual focus is on the first button (you can even see some dotted lines inside it).
If you press space or enter it triggers the 1st not the 2nd, told to be default.

Code: Select all

Gui, New
Gui, Add, Text, , Text
Gui, Add, Button, , Yes
Gui, Add, Button, x+m Default, No
Gui, Show

return

ButtonNo:
	MsgBox, No
return

ButtonYes:
	MsgBox, Yes
return

Edit:
I have a fixed it temporarily through GuiControl, Focus but I don't think it should be needed.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Default button not acting as default

20 Jun 2021, 05:53

https://www.autohotkey.com/docs/commands/GuiControls.htm#DefaultButton
The example above includes the word Default in its Options to make "OK" the default button. The default button's action is automatically triggered whenever the user presses Enter, except when the keyboard focus is on a different button or a multi-line edit control having the WantReturn style.
https://docs.microsoft.com/en-us/windows/win32/controls/button-styles
BS_DEFPUSHBUTTON wrote:Creates a push button that behaves like a BS_PUSHBUTTON style button, but has a distinct appearance. If the button is in a dialog box, the user can select the button by pressing the ENTER key, even when the button does not have the input focus. This style is useful for enabling the user to quickly select the most likely (default) option.
theres nothing to suggest that applying the Default ahk-option at the control's creation(or otherwise) should also shift focus over to it(or away from it, or do anything else at all)

in other words, ure unfoundedly assuming it should behave a certain way, when it in fact doesnt
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: Default button not acting as default

20 Jun 2021, 06:14

I guess from a certain point that makes sense.
But I was trying to make sense of the that and I tested this, which acts different:

Code: Select all

Gui, New
Gui, Add, Edit, , Text
Gui, Add, Button, , Yes
Gui, Add, Button, x+m Default, No
Gui, Show

return

ButtonNo:
	MsgBox, No
return

ButtonYes:
	MsgBox, Yes
return
This one actually focuses on the default.
Shouldn't both scripts focus on the same button, even if it's not the default?

Edit:
Actually it actually shouldn't unfocus the edit now that I think of it better.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Default button not acting as default

20 Jun 2021, 06:32

That is the expected behaviour in v1.
When the window is shown for the first time, the first input-capable control that has the Tabstop style (which most control types have by default) will have keyboard focus.
Source: Gui - Syntax & Usage | AutoHotkey
v2.0-a134 and later focus the Default button instead of the first input-capable control if it's also a Button.
safetycare wrote:This one actually focuses on the default.
No, it focuses the Edit control.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Default button not acting as default

20 Jun 2021, 06:32

i dont know. seems to me, focus is given to the first focus-receiving-capable control. whether that is by windows' or ahk's design, i cant say(but its probably windows)
e: well, there u have it. from the man himself :lol:
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: Default button not acting as default

20 Jun 2021, 06:35

lexikos wrote:
20 Jun 2021, 06:32
No, it focuses the Edit control.
I was more focused on testing through Enter than what I was seeing, which caught the "No"...

I have to think about it... :roll:
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: Default button not acting as default

20 Jun 2021, 07:04

I was (clearly) confusing focus with default.

I think I'm more clear now. I think the summary is:
Focus is usually what rules, but if the Enter has no meaning in the focused control, the one with Default takes care of the action.
And Default also gives a border style only for visual purposes.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Frogrammer, gongnl and 274 guests