Tab to Cancel button on GUI continues script with Enter key

Get help with using AutoHotkey and its commands and hotkeys
hidefguy
Posts: 57
Joined: 11 Apr 2017, 20:42

Tab to Cancel button on GUI continues script with Enter key

13 Mar 2018, 10:44

Is it possible to insert a command in here, that when I tab to GUI Cancel button and press Enter, the script returns and not continue? Same way it does when pressing Esc or clicking Cancel

Gui, Add, Text, x12 y20 w80 h30, Date
Gui, Add, Edit, x112 y20 w80 h20 vDate,
Gui, Add, Button, x40 y270 w80 h30 , OK
Gui, Add, Button, x180 y270 w80 h30 , Cancel
Gui, Show, x800 y400 h325 w300, INPUT
Return
ButtonCancel:
GuiEscape:
Gui, Destroy
Return
ButtonOK:
Enter::
NumpadEnter::
Gui, Submit
Gui, Destroy
Last edited by hidefguy on 14 Mar 2018, 22:35, edited 3 times in total.
hidefguy
Posts: 57
Joined: 11 Apr 2017, 20:42

Re: Tab to Cancel on GUI continues script

13 Mar 2018, 11:27

Tried this. Script still continues

Gui, Add, Text, x12 y20 w80 h30, Date
Gui, Add, Edit, x112 y20 w80 h20 vDate,
Gui, Add, Button, x40 y270 w80 h30 , OK
Gui, Add, Button, x180 y270 w80 h30 , Cancel
Gui, Add, Button, x180 y270 w80 h30 gButtonCancel, Cancel
Gui, Show, x800 y400 h325 w300, INPUT
Return
ButtonCancel:
gButtonCancel:
GuiEscape:
Gui, Destroy
Return
ButtonOK:
Enter::
NumpadEnter::
Gui, Submit
Gui, Destroy
hidefguy
Posts: 57
Joined: 11 Apr 2017, 20:42

Re: Tab to Cancel on GUI continues script

14 Mar 2018, 07:48

Searched this forum into the abyss, and still can’t figure this out. Like any Windows program, I want the capability to Tab my way to Cancel button on input box and execute the cancel button by pressing Enter.

Is this not a trivial task in AHK? :)
just me
Posts: 7027
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Tab to Cancel on GUI continues script

14 Mar 2018, 09:08

Code: Select all

ButtonCancel:
GuiEscape:
Gui, Destroy
ExitApp ; Gui applications are persistent and need ExitApp instead of Return/Exit
hidefguy
Posts: 57
Joined: 11 Apr 2017, 20:42

Re: Tab to Cancel on GUI continues script

14 Mar 2018, 09:32

TY, but this doesn't relate to using the enter key when tabbing over the Cancel button. This exits the entire script. AHK allows for only one Enter:: command which I have reserved to start the remaining script. The goal is to reference the enter key to cancel Gui only when tabbed over the cancel button.
User avatar
divanebaba
Posts: 794
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Tab to Cancel on GUI continues script

14 Mar 2018, 10:44

Hi.
If your enter-key is remapped or used by a hotstring, you can add a query to find active focus. If active focus is your cancel-button, you can do your stuff.
Look for ControlGetFocus.
Like code below:

Code: Select all

Enter::
ControlGetFocus, myFocus, myWindowTitle
if (myFocus = "Button7")
...
else
...
hidefguy
Posts: 57
Joined: 11 Apr 2017, 20:42

Re: Tab to Cancel on GUI continues script

14 Mar 2018, 12:34

Tried this but get else not recognized error

Gui, Add, Text, x12 y20 w80 h30, Date
Gui, Add, Edit, x112 y20 w80 h20 vDate,
Gui, Add, Button, x40 y270 w80 h30 , OK
Gui, Add, Button, x180 y270 w80 h30 , Cancel
Gui, Show, x800 y400 h325 w300, INPUT
Return
ButtonCancel:
GuiEscape:
Gui, Destroy
Return
Enter::
NumpadEnter::
ControlGetFocus, myFocus, INPUT
if (myFocus = "ButtonCancel")
else
ButtonOK:
Gui, Submit
Gui, Destroy
User avatar
divanebaba
Posts: 794
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Tab to Cancel on GUI continues script

14 Mar 2018, 12:59

Hi.
Try to find the classNN of your CancelButton with WindowSpy or ACC-Viewer.
It should be "Button2" or another number, depending on the amount of buttons or viewer, you use.
Try first WindowSpy. If this fails, try ACC-Viewer.

EDIT: A little fault in your code. Replace with following:

Code: Select all

...
if (myFocus = "Button2") 
msgbox Yes, it is Button2
else
msgbox No, it was not Button2
...
hidefguy
Posts: 57
Joined: 11 Apr 2017, 20:42

Re: Tab to Cancel on GUI continues script

14 Mar 2018, 22:22

divanebaba
This variation of your snippet worked well. Tabbing/pressing Enter key to Button 2 (Cancel or No), destroys and returns GUI, but with Enter key at Button 1 (OK or Yes), or pressing Enter anytime/anywhere it will generate another GUI asking to continue, or not. This way the script doesn't run away. Alternately, the Esc key can be used to exit any GUI, with command GuiEscape:
I'm sure it will be useful to someone in the future. Thank you all for your help.

Code: Select all

Gui, Add, Text, x12 y20 w80 h30, Date
Gui, Add, Edit, x112 y20 w80 h20 vDate,
Gui, Add, Button, x40 y270 w80 h30 , OK
Gui, Add, Button, x180 y270 w80 h30 , Cancel
Gui, Show, x800 y400 h325 w300, INPUT
Return
ButtonCancel:
GuiEscape:
Gui, Destroy
Return
Enter::
NumpadEnter::
ControlGetFocus, myFocus1, INPUTS
if (myFocus1 = "Button1")
{
    Goto, ButtonYes
}
else
if (myFocus1 = "Button2")
{
    Gui, Destroy
    Return
}
ControlGetFocus, myFocus2, CONTINUE
if (myFocus2 = "Button1")
{
    Goto, ButtonYes
}
else
if (myFocus2 = "Button2")
{
    Gui, Destroy
    ExitApp
}
Gui, Submit
Gui, Destroy
Gui, Add, Text, x40 y30 w120 h50, Continue running script?
Gui, Add, Button, x40 y90 w80 h30 , Yes
Gui, Add, Button, x180 y90 w80 h30 , No
Gui, Show, x800 y500 h150 w300, CONTINUE
Return
Gui, Destroy
ButtonOK:
ButtonYes:
Gui, Submit
Gui, Destroy 
sleep, 1000

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler, Google [Bot], marwanco, Preed, SilasDeVis, swagfag, vsub and 89 guests