GUI button loses default after coming back from any other window that gains focus

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JoeWinograd
Posts: 2202
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

GUI button loses default after coming back from any other window that gains focus

07 Mar 2019, 20:50

AHK 1.1.30.01 U32
W7 Pro 64-bit

I don't know if this is a bug or not, but decided to post it here first before submitting a bug report.

I have a script with three buttons. Any one of them can be marked as the Default when the user clicks on it (so that it is highlighted), while the other two are set as not Default (so that they are not highlighted). It works fine when the GUI first displays and it works fine when the user clicks a button, but here's the problem. When the user clicks any other open window, thereby taking focus away from the AHK window, and then comes back to the AHK window simply by clicking its title bar (not one of the buttons), the button that had been highlighted is not!

Here's a small script that I created to reproduce the problem:

Code: Select all

Gui,Font,s12 w1000
Gui,Add,Button,x10 y10 w30 h30 gLetterA vLetterA -Default,A
Gui,Add,Button,x50 y10 w30 h30 gLetterB vLetterB +Default,B
Gui,Add,Button,x90 y10 w30 h30 gLetterC vLetterC -Default,C
Gui,Show,w250 h50
Return

LetterA:
GuiControl,-Default,LetterB
GuiControl,-Default,LetterC
GuiControl,+Default,LetterA
Return

LetterB:
GuiControl,-Default,LetterC
GuiControl,-Default,LetterA
GuiControl,+Default,LetterB
Return

LetterC:
GuiControl,-Default,LetterA
GuiControl,-Default,LetterB
GuiControl,+Default,LetterC
Return

GuiClose:
GuiEscape:
ExitApp
When you run it, the Letter B is Default (highlighted)...click A, click C, click B...it all works fine...the clicked letter is Default (highlighted).

Now run it again. The Letter B is Default (highlighted). Click the title bar of any other window. Now click the title bar of the AHK window. The GUI now shows Letter A as Default (highlighted)! Even though no user action took place in the GUI, the highlighted button changed simply because the window lost and regained focus.

I appreciate your thoughts on this one. Regards, Joe
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: GUI button loses default after coming back from any other window that gains focus

07 Mar 2019, 22:56

Hi!
I tried to throw in some GuiControl,Focus
Is it what you are after :?:

Code: Select all

Gui,Font,s12 w1000
Gui,Add,Button,x10 y10 w30 h30 gLetterA vLetterA -Default,A
Gui,Add,Button,x50 y10 w30 h30 gLetterB vLetterB +Default,B
Gui,Add,Button,x90 y10 w30 h30 gLetterC vLetterC -Default,C
GuiControl,Focus,LetterB
Gui,Show,w250 h50
Return

LetterA:
GuiControl,-Default,LetterB
GuiControl,-Default,LetterC
GuiControl,+Default,LetterA
GuiControl,Focus,LetterA
Return

LetterB:
GuiControl,-Default,LetterC
GuiControl,-Default,LetterA
GuiControl,+Default,LetterB
GuiControl,Focus,LetterB
Return

LetterC:
GuiControl,-Default,LetterA
GuiControl,-Default,LetterB
GuiControl,+Default,LetterC
GuiControl,Focus,LetterC
Return

GuiClose:
GuiEscape:
ExitApp
just me
Posts: 9473
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: GUI button loses default after coming back from any other window that gains focus

08 Mar 2019, 05:07

Hi,

the GuiControl, Focus, LetterB before the Gui, Show, ... seems to be all you need:

Code: Select all

#NoEnv
Gui, Font, s12 w1000
Gui, Add, Button, x10 y10 w30 h30 gLetterA vLetterA , A
Gui, Add, Button, x50 y10 w30 h30 gLetterB vLetterB +Default, B
Gui, Add, Button, x90 y10 w30 h30 gLetterC vLetterC , C
GuiControl, Focus, LetterB
Gui,Show,w250 h50
Return

LetterA:
LetterB:
LetterC:
ToolTip, %A_ThisLabel%
SetTimer, ToolTipOff, 1000
Return

ToolTipOff:
ToolTip
Return

GuiClose:
GuiEscape:
ExitApp
Without it, the button LetterA gains the focus when the script's window is shown. If you activate another window and reactivate the script's window without focusing a control, AHK sets the keyboard focus to LetterA again. At this moment the button also gets the default button style as if a user clicked the button. I'm not sure wether this behaviour can be called a bug.

Related: GuiControl, Focus
User avatar
JoeWinograd
Posts: 2202
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: GUI button loses default after coming back from any other window that gains focus

08 Mar 2019, 08:32

Ah, Focus...works a charm! My thanks to both of you...perfect! And thanks for the link to the GuiControl, Focus thread...very interesting! Regards, Joe

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Jose-phil, OrangeCat, Quinnt, slowwd and 378 guests