Press Enter - Jump to specific control

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1752
Joined: 16 Oct 2013, 13:53

Press Enter - Jump to specific control

12 Nov 2018, 16:26

I have a GUI-Window.
It have some controls, like this demo-script.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
#SingleInstance Force
; #MaxThreadsPerHotkey 2
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Gui 1: Default
Gui Color, White

Gui Font, s20 cBlack Normal, Verdana
Gui Add, Text, x50 y100 w120 h40 vText1, Text1 .:
Gui Font, s24 cGreen Bold, Verdana
Gui Add, Edit, x180 y80 w280 h50 +Left vInp1

Gui Font, s20 cBlack Normal, Verdana
Gui Add, Text, x50 y175 w120 h40 vText2, Text2 .:
Gui Font, s24 cGreen Bold, Verdana
Gui Add, Edit, x180 y155 w280 h50 +Left vInp2

Gui Font, s20 cBlack Normal, Verdana
Gui Add, Text, x50 y250 w120 h40 vText3, Text3 .:
Gui Font, s24 cGreen Bold, Verdana
Gui Add, Edit, x180 y230 w280 h50 +Left vInp3

Gui Font, s20 cBlack Bold, Verdana
Gui Add, Button, x220 y350 w200 h150 vRegistrera, Registrera!

Gui Show, x131 y91 h584 w625, Test window
Return

GuiClose:
ExitApp
If the first field ends with Enter, I want to point on the second field.
And if the third field ends with Enter, I want to get focus on the button.
A similar function as the TAB button, but with the ability to skip any control field.

Is there any easy way to jump with Enter in AHK-script?
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: Press Enter - Jump to specific control

12 Nov 2018, 17:15

made complicated ...

Code: Select all

;-- 
Gui,2:default
Gui,2: Font, CDefault, FixedSys
hotkey,enter,a2,on
loop,3
   Gui,2:Add,Edit, xp  y+10 h24 w100  left cBlue v%a_index% gA1
Gui,2:add,Button,xp y+10 h25 w200 v4 gA1 ,TEST   
Gui,2: Show, x100 y5  w220,Enter Test
return

2Guiclose:
exitapp
esc::exitapp

a1:
Gui,2:submit,nohide
r:=a_GuiControl
if (r=4)
    {
	goto,Label1
	r:=1
	GuiControl,2:Focus,%r%
	}
return

a2:
Gui,2:submit,nohide
textx:= % %r%
;msgbox, 262208, ,This is`nR=%r%`nText= %textx% , 1
r:=(r+1)
GuiControl,2:Focus,%r%
if (r=4)
    {
	goto,Label1
	r:=1
	GuiControl,2:Focus,%r%
	}
return

Label1:
msgbox, 262208, ,You clicked button TEST,3
GuiControl,2:Focus,1
return

;===================================================
Albireo
Posts: 1752
Joined: 16 Oct 2013, 13:53

Re: Press Enter - Jump to specific control

13 Nov 2018, 07:19

Thanks! (it works!)
The challenge is that there are a lot of other controls like checkboxes, buttons and so on.
I like to give the controls proper names. (instead of 1,2,3,4 ;) )
But I have understand the technic.

The problem becomes a bit more obvious/complicated if the desire is to jump in another order.
for example.:
from "input field 1" to "input field 3"
from "input field 3" to "input field 2"
and finally
from "input field 2" to the "Button"

Perhaps the jump order, could be based on a Match List?

Code: Select all

MatchList := 1,3,2,4
But I only find that/if the control is in the list, not which control comes next.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
How difficult would it be to use Objects in this case?

Code: Select all

JumpOrder := [1,3,2,4]
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Press Enter - Jump to specific control

13 Nov 2018, 10:28

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
#SingleInstance Force
; #MaxThreadsPerHotkey 2
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

FocusControls := {Inp1: "Inp3", Inp2: "Registrera", Inp3: "Inp2"}

Gui 1: Default

Gui +hwndHGUI
GroupAdd, AhkGui, ahk_id %HGUI%

Gui Color, White

Gui Font, s20 cBlack Normal, Verdana
Gui Add, Text, x50 y100 w120 h40 vText1, Text1 .:
Gui Font, s24 cGreen Bold, Verdana
Gui Add, Edit, x180 y80 w280 h50 +Left vInp1

Gui Font, s20 cBlack Normal, Verdana
Gui Add, Text, x50 y175 w120 h40 vText2, Text2 .:
Gui Font, s24 cGreen Bold, Verdana
Gui Add, Edit, x180 y155 w280 h50 +Left vInp2

Gui Font, s20 cBlack Normal, Verdana
Gui Add, Text, x50 y250 w120 h40 vText3, Text3 .:
Gui Font, s24 cGreen Bold, Verdana
Gui Add, Edit, x180 y230 w280 h50 +Left vInp3

Gui Font, s20 cBlack Bold, Verdana
Gui Add, Button, x220 y350 w200 h150 vRegistrera, Registrera!

Gui Show, x131 y91 h584 w625, Test window
Return

GuiClose:
ExitApp

#IfWinActive ahk_group AhkGui
~Enter::
GuiControlGet, Focused, FocusV
If (SetFocus := FocusControls[Focused])
   GuiControl, Focus, %SetFocus%
Return
#IfWinActive
Albireo
Posts: 1752
Joined: 16 Oct 2013, 13:53

Re: Press Enter - Jump to specific control

13 Nov 2018, 14:16

Thanks!
I wanted something similar to this solution.
The only thing is, when Control3 has a value and the "Enter" was pressed, I want to focus on the button.
And if the "Enter" is pressed again, "Enter pressed the button - and the program is going on….

I try to understand what happens in the ahk-script.
What's happening on this lines?

Code: Select all

Gui +hwndHGUI
GroupAdd AhkGui, ahk_id %HGUI%
and what happens on this lines?

Code: Select all

#IfWinActive ahk_group AhkGui
~Enter::
	GuiControlGet Focused, FocusV

	If (SetFocus := FocusControls[Focused])
		GuiControl Focus, %SetFocus%
Return
#IfWinActive
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Press Enter - Jump to specific control

14 Nov 2018, 06:16

Code: Select all

; get a handle to the GUI window and store it in HGUI
Gui +hwndHGUI
; add the GUI window to the window group AhkGui
GroupAdd AhkGui, ahk_id %HGUI%

Code: Select all

; start of context-sensitive hotkeys, they will be triggered only if one (the only one) window of the group AhkGui is active
#IfWinActive ahk_group AhkGui
~Enter::
	; get the name of the currently focused control
	GuiControlGet Focused, FocusV 
	; if there's an entry in FocusControls for the focused control, SetFocus will be set to the name of the control which shall gain the focus
	; otherwise SetFocus will be made empty
	If (SetFocus := FocusControls[Focused])
		; set the focus to the new control
		GuiControl Focus, %SetFocus% 
Return
; end of context-sensitive hotkeys
#IfWinActive
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: Press Enter - Jump to specific control

16 Nov 2018, 03:22

@just me , thank you for the good example
I tried to click the button (Calculator) after 3 inputs finished , is this ok ?

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
#SingleInstance Force
; #MaxThreadsPerHotkey 2
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
FocusControls := {Inp1: "Inp3", Inp2: "Calc", Inp3: "Inp2"}
Wintitle:="Test Window"
Gui 1: Default
Gui +hwndHGUI
GroupAdd, AhkGui, ahk_id %HGUI%
Gui Color, White
Gui Font, s20 cBlack Normal, Verdana
Gui Add, Text, x50 y100 w120 h40 vText1, Text1 .:
Gui Font, s24 cGreen Bold, Verdana
Gui Add, Edit, x180 y80 w280 h50 +Left vInp1
Gui Font, s20 cBlack Normal, Verdana
Gui Add, Text, x50 y175 w120 h40 vText2, Text2 .:
Gui Font, s24 cGreen Bold, Verdana
Gui Add, Edit, x180 y155 w280 h50 +Left vInp2
Gui Font, s20 cBlack Normal, Verdana
Gui Add, Text, x50 y250 w120 h40 vText3, Text3 .:
Gui Font, s24 cGreen Bold, Verdana
Gui Add, Edit, x180 y230 w280 h50 +Left vInp3
Gui Font, s20 cBlack Bold, Verdana
Gui Add, Button, x220 y350 w200 h150 vCalc gA1, Calculator
Gui Show, x131 y91 h584 w625,%wintitle%
Return
GuiClose:
ExitApp

a1:
run,calc
return

#IfWinActive ahk_group AhkGui
~Enter::
GuiControlGet, Focused, FocusV
If (SetFocus := FocusControls[Focused])
   {
   msgbox, 262208, ,Setfocus=%setfocus%,1  ;- < for test
      GuiControl, Focus, %SetFocus%
   if (setfocus="Calc")
   ControlClick,Calculator,%wintitle%
   }
Return
#IfWinActive
Albireo
Posts: 1752
Joined: 16 Oct 2013, 13:53

Re: Press Enter - Jump to specific control

19 Nov 2018, 17:43

I think so.
Why are you (garry) unsure?
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: Press Enter - Jump to specific control

20 Nov 2018, 14:43

Why are you (garry) unsure?
script is from user 'just me' , I hope he agrees or has maybe a better solution

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 165 guests