GUI: getting controls to respond to Enter (including controls within tab controls)

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

GUI: getting controls to respond to Enter (including controls within tab controls)

31 May 2018, 21:22

tl;dr why does my tab control script (with hidden button controls) work the way it does

- Generally speaking in AutoHotkey, if you create a g-label for a control, it does not receive an event when Enter is pressed.
- The documentation has the same piece of advice for listview/treeview controls, which is applicable to other control types also:
ListView (GUI)
https://autohotkey.com/docs/commands/ListView.htm
To detect when the user has pressed Enter while a ListView has focus, use a default button (which can be hidden if desired).
TreeView (GUI)
https://autohotkey.com/docs/commands/TreeView.htm
To detect when the user has pressed Enter while a TreeView has focus, use a default button (which can be hidden if desired).
- However, what about when you have controls within tab controls, what to do then? I did some experimenting, but I'm not really sure why it works the way it does. I.e. one hidden button for the entire window, or one hidden button per tab. Thanks for reading.

Code: Select all

;testing controls within tab controls and receiving Enter presses
#SingleInstance force

;with the 100 0 configuration, enter only works on tab 1
;with the 110 0 configuration, enter only works on tab 2
;with the 111 0 configuration, enter only works on tab 3
;with the 000 1 configuration, enter works on all tabs
;with the 111 1 configuration, enter works on all tabs
vDo1 := 1
vDo2 := 1
vDo3 := 0
vDoAll := 0

Gui, New, +HwndhGui, MyWinTitle
;if vDoAllX
;	Gui, Add, Button, Hidden Default gSubBtnAllX, OK
Gui, Add, Tab3, h100, TAB 1|TAB 2|TAB 3

Gui, Add, ComboBox, +Simple gSubCbx1, A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
if vDo1
	Gui, Add, Button, Hidden Default gSubBtn1, OK

Gui, Tab, 2
Gui, Add, ComboBox, +Simple gSubCbx2, A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
if vDo2
	Gui, Add, Button, Hidden Default gSubBtn2, OK

Gui, Tab, 3
Gui, Add, ComboBox, +Simple gSubCbx3, A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
if vDo3
	Gui, Add, Button, Hidden Default gSubBtn3, OK

Gui, Tab ;future controls are not part of any tab control
if vDoAll
	Gui, Add, Button, Hidden Default gSubBtnAll, OK

Gui, Show, w300 h300
return

;==================================================

SubCbx1:
SubBtn1:
SubCbx2:
SubBtn2:
SubCbx3:
SubBtn3:
SubBtnAllX:
SubBtnAll:
;MsgBox, % A_ThisLabel "`r`n" A_Gui "`r`n[" A_GuiEvent " / " A_GuiControlEvent "]`r`n" A_GuiControl
ToolTip, % A_ThisLabel "`r`n" A_Gui "`r`n[" A_GuiEvent " / " A_GuiControlEvent "]`r`n" A_GuiControl
return

GuiClose:
ExitApp
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jballi
Posts: 692
Joined: 29 Sep 2013, 17:34

Re: GUI: getting controls to respond to Enter (including controls within tab controls)

02 Jun 2018, 00:43

It took me a while to figure out what you were doing here but it appears that this is not a Tab control issue. Well, not really. There can only be one default button. When you create more that one "default" button, only the last one created is the default.

You have already created one workaround by adding a button after the tab control. That seems to work the best for this example. Another workaround is to change the default button when the tab changes. Something like this...

Code: Select all

#NoEnv

;testing controls within tab controls and receiving Enter presses
#SingleInstance force

;with the 100 0 configuration, enter only works on tab 1
;with the 110 0 configuration, enter only works on tab 2
;with the 111 0 configuration, enter only works on tab 3
;with the 000 1 configuration, enter works on all tabs
;with the 111 1 configuration, enter works on all tabs
vDo1 := 1
vDo2 := 1 
vDo3 := 1
vDoAll := 0

Gui, New, +HwndhGui, MyWinTitle
;if vDoAllX
;	Gui, Add, Button, Hidden Default gSubroutineButtonAllX, OK
Gui, Add, Tab3,vMyTab gTabControlAction,TAB 1|TAB 2|TAB 3

Gui, Add, ComboBox, +Simple gSubroutineComboBoxx1, A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
if vDo1
	Gui, Add, Button,Default vTab1DefaultButton gSubroutineButton1,OK

Gui, Tab, 2
Gui, Add, ComboBox, +Simple gSubroutineComboBoxx2, A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
if vDo2
	Gui, Add, Button,vTab2DefaultButton gSubroutineButton2,OK

Gui, Tab, 3
Gui, Add, ComboBox, +Simple gSubroutineComboBoxx3, A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
if vDo3
	Gui, Add, Button,vTab3DefaultButton gSubroutineButton3,OK

;-- After tabs
Gui, Tab
if vDoAll
	Gui, Add, Button, Default gSubroutineButtonAll, OK

Gui, Show
return

;==================================================

SubroutineComboBoxx1:
SubroutineButton1:
SubroutineComboBoxx2:
SubroutineButton2:
SubroutineComboBoxx3:
SubroutineButton3:
SubroutineButtonAllX:
SubroutineButtonAll:
;MsgBox, % A_ThisLabel "`r`n" A_Gui "`r`n[" A_GuiEvent " / " A_GuiControlEvent "]`r`n" A_GuiControl
ToolTip, % A_ThisLabel "`r`n" A_Gui "`r`n[" A_GuiEvent " / " A_GuiControlEvent "]`r`n" A_GuiControl
SetTimer ClearToolTip,1000
return

GUIEscape:
GuiClose:
ExitApp
return


ClearToolTip:
SetTimer %A_ThisLabel%,Off
ToolTip
return


TabControlAction:
gui Submit,NoHide
If (MyTab="TAB 1")
    GUIControl +Default,Tab1DefaultButton
else if (MyTab="TAB 2")
    GUIControl +Default,Tab2DefaultButton
else
    GUIControl +Default,Tab3DefaultButton

return
Another workaround is to create hotkeys for the Enter and NumpadEnter. It will work for this example but may not be work well if other controls need the Enter/NumpadEnter key (Ex: Edit control) although you could send the key in those cases.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: GUI: getting controls to respond to Enter (including controls within tab controls)

02 Jun 2018, 00:54

- Thanks.
- What I'm doing at present is to use:
ControlGet, vTabNum, Tab,,, % "ahk_id " hTab
and:
ControlGetFocus, vCtlClassNN, % "ahk_id " hGui
after the hidden default button is triggered, and then perform an action based on the tab number and focused control.
- I had thought that, potentially, each tab would be treated as a separate window with its own separate default control.
- I had thought that each time the tab changes, the default button could be changed, but how to keep track of the tab changing. (You've come up with a fix with your TabControlAction sub.)
- Also, if you add hidden default buttons to tab 1 and tab 2, what happens to the button in tab 1?
- I'm tempted to say that if you built the GUI from the ground up, you'd understand why it doesn't work. But from a ready-built GUI set of commands and subcommands it's harder to see.
Last edited by jeeswg on 02 Jun 2018, 01:29, edited 2 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
icuurd12b42
Posts: 202
Joined: 14 Aug 2016, 04:08

Re: GUI: getting controls to respond to Enter (including controls within tab controls)

02 Jun 2018, 01:07

are you trying the change the default button to a button on each tab?

apparently you can use the send message winapi to change the button style at run time. all you would need is to create the default button on the first/visible tab and non default buttons on the other tabs and switch the button styles when the tab changes...

http://forums.codeguru.com/showthread.p ... uttons-MFC
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: GUI: getting controls to respond to Enter (including controls within tab controls)

02 Jun 2018, 01:12

- @icuurd12b42: Great link, thanks for sharing. It's got all of those nice under-the-hood details.
- It reminds me of part of why I asked the question. If the default buttons could *not* be hidden, what to do then. Thanks to both of you for suggesting answers.
- (It's a curious fusion of two issues: default controls + tab controls. But as the link shows, simply changing the default control is an interesting challenge too.)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: andymbody, bartolllomeo, HiSoKa, mikeyww, rawskull7, Skrell, twiz, whereyomomsat_ and 44 guests