Jump to content

about Focus


Manimal
 Share

Recommended Posts

I have a simple window with 2 inputs and a button.

I want to use the ENTER (or return key) to change between inputs and button.

Use HotSetKey to capture the ENTER key and SEND a tab to change focus, but when i press ENTER in the button, just jumps to 1st input again, not calc.

Is there a function to know where is my focus? In which control?

So when i have my focus in the button, not send tab, just do the calc.

Local $Input1, $Input2
Local $Form1, $Button1, $Msg
Local $Label1, $Label2, $Label4, $Label5, $Label7, $Label8, $Label9

HotKeySet("{ENTER}", "SelectControl")
$Form1 = GUICreate("My Window)", 436, 228, 311, 226)

$Label2 = GUICtrlCreateLabel("Number 1:", 64, 72, 92, 17)
$Input2 = GUICtrlCreateInput("", 168, 68, 51, 21)

$Label1 = GUICtrlCreateLabel("Number 2:", 64, 112, 37, 17)
$Input1 = GUICtrlCreateInput("", 168, 108, 51, 21)

$Label4 = GUICtrlCreateLabel("Result 1:", 64, 156, 23, 17)
$Label6 = GUICtrlCreateLabel("", 188, 152, 80, 17)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")

$Label5 = GUICtrlCreateLabel("Result 2:", 64, 192, 80, 17)
$Label7 = GUICtrlCreateLabel("", 188, 188, 80, 17)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")

$Button1 = GUICtrlCreateButton("Calc", 296, 106, 75, 25, 0)
GUISetState(@SW_SHOW)

Do 
    $Msg = GUIGetMsg()
   If $Msg = $Button1 Then
      Calcula()
    EndIf
Until $Msg = $GUI_EVENT_CLOSE

Func Calcula ()
   ...
EndFunc

Func SelectControl ()
   If WinActive($Form1) Then
      Send("{TAB}")
    EndIf
EndFunc
Link to comment
Share on other sites

I have a simple window with 2 inputs and a button.

I want to use the ENTER (or return key) to change between inputs and button.

Use HotSetKey to capture the ENTER key and SEND a tab to change focus, but when i press ENTER in the button, just jumps to 1st input again, not calc.

Is there a function to know where is my focus? In which control?

So when i have my focus in the button, not send tab, just do the calc.

Local $Input1, $Input2
Local $Form1, $Button1, $Msg
Local $Label1, $Label2, $Label4, $Label5, $Label7, $Label8, $Label9

HotKeySet("{ENTER}", "SelectControl")
$Form1 = GUICreate("My Window)", 436, 228, 311, 226)

$Label2 = GUICtrlCreateLabel("Number 1:", 64, 72, 92, 17)
$Input2 = GUICtrlCreateInput("", 168, 68, 51, 21)

$Label1 = GUICtrlCreateLabel("Number 2:", 64, 112, 37, 17)
$Input1 = GUICtrlCreateInput("", 168, 108, 51, 21)

$Label4 = GUICtrlCreateLabel("Result 1:", 64, 156, 23, 17)
$Label6 = GUICtrlCreateLabel("", 188, 152, 80, 17)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")

$Label5 = GUICtrlCreateLabel("Result 2:", 64, 192, 80, 17)
$Label7 = GUICtrlCreateLabel("", 188, 188, 80, 17)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")

$Button1 = GUICtrlCreateButton("Calc", 296, 106, 75, 25, 0)
GUISetState(@SW_SHOW)

Do 
    $Msg = GUIGetMsg()
   If $Msg = $Button1 Then
      Calcula()
    EndIf
Until $Msg = $GUI_EVENT_CLOSE

Func Calcula ()
   ...
EndFunc

Func SelectControl ()
   If WinActive($Form1) Then
      Send("{TAB}")
    EndIf
EndFunc
You need ControlGetFocus. I think ControlFocus would also make your function more reliable than using Tab.

Note that you need to call Calcula in SelectControl as well as your main while loop.

#include <windowsconstants.au3>
#include <GUIConstantsEx.au3>

Local $Input1, $Input2
Local $Form1, $Button1, $Msg
Local $Label1, $Label2, $Label4, $Label5, $Label7, $Label8, $Label9

HotKeySet("{ENTER}", "SelectControl")
$Form1 = GUICreate("My Window)", 436, 228, 311, 226)

$Label2 = GUICtrlCreateLabel("Number 1:", 64, 72, 92, 17)
$Input2 = GUICtrlCreateInput("", 168, 68, 51, 21)

$Label1 = GUICtrlCreateLabel("Number 2:", 64, 112, 37, 17)
$Input1 = GUICtrlCreateInput("", 168, 108, 51, 21)

$Label4 = GUICtrlCreateLabel("Result 1:", 64, 156, 23, 17)
$Label6 = GUICtrlCreateLabel("", 188, 152, 80, 17)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")

$Label5 = GUICtrlCreateLabel("Result 2:", 64, 192, 80, 17)
$Label7 = GUICtrlCreateLabel("", 188, 188, 80, 17)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")

$Button1 = GUICtrlCreateButton("Calc", 296, 106, 75, 25, 0)
GUISetState(@SW_SHOW)

Do
    $Msg = GUIGetMsg()
    If $Msg = $Button1 Then
        Calcula();needed here if button clicked
    EndIf
Until $Msg = $GUI_EVENT_CLOSE

Func Calcula()
ConsoleWrite( Number(GUICtrlRead($Input1)) * Number(GUICtrlRead($Input2)) & @CRLF)
EndFunc  ;==>Calcula

Func SelectControl()
    If WinActive($Form1) Then
        $cf = ControlGetFocus("My Window", "")
        Switch $cf
            Case "Edit1"
                ControlFocus($Form1, "", "Edit2")
            Case "Edit2"
                ControlFocus($Form1, "", "Button1")
            Case "Button1"
                Calcula();needed here if Enter pressed
        EndSwitch
    ;ConsoleWrite($cf & @CRLF)
    Else;the window with focus should receive the Enter key
        HotKeySet("{ENTER}");stop trapping th eEnter key
        Send("{Enter}")
        HotKeySet("{ENTER}", "SelectControl");reset hot key
    EndIf

EndFunc  ;==>SelectControl
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

It would probably be better in this circumstance to use GUISetAccelerators instead of HotKeySet, since you don't need a system-wide hotkey, only a key for your GUI. This would avoid the 'If WinActive()' check and unsetting / resetting the hotkey.

Edited by wraithdu
Link to comment
Share on other sites

It would probably be better in this circumstance to use GUISetAccelerators instead of HotKeySet, since you don't need a system-wide hotkey, only a key for your GUI. This would avoid the 'If WinActive()' check and unsetting / resetting the hotkey.

Yes that's a good point. To have an accelerator for three controls which is the same key adds its own complications. I think it would be best in this case to create a dummy control and use Enter as an accelerator for the dummy. Like this

#include <windowsconstants.au3>
#include <GUIConstantsEx.au3>

Local $Input1, $Input2
Local $Form1, $Button1, $Msg
Local $Label1, $Label2, $Label4, $Label5, $Label7, $Label8, $Label9


$Form1 = GUICreate("My Window)", 436, 228, 311, 226)

$Label2 = GUICtrlCreateLabel("Number 1:", 64, 72, 92, 17)
$Input2 = GUICtrlCreateInput("", 168, 68, 51, 21)

$Label1 = GUICtrlCreateLabel("Number 2:", 64, 112, 37, 17)
$Input1 = GUICtrlCreateInput("", 168, 108, 51, 21)

$Label4 = GUICtrlCreateLabel("Result 1:", 64, 156, 23, 17)
$Label6 = GUICtrlCreateLabel("", 188, 152, 80, 17)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Label5 = GUICtrlCreateLabel("Result 2:", 64, 192, 80, 17)
$Label7 = GUICtrlCreateLabel("", 188, 188, 80, 17)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$dum = GUICtrlCreateDummy()
$Button1 = GUICtrlCreateButton("Calc", 296, 106, 75, 25, 0)
$dum = GUICtrlCreateDummy()
GUISetState(@SW_SHOW)
DIm $hk[1][2] = [["{ENTER}",$dum]]
GUISetAccelerators($hk,$Form1)
Do
    $Msg = GUIGetMsg()
    If $Msg = $Button1 Then
        Calcula();needed here if button clicked
    EndIf
    
    if $msg = $dum then
        SelectControl()
    EndIf
Until $Msg = $GUI_EVENT_CLOSE

Func Calcula()
ConsoleWrite( Number(GUICtrlRead($Input1)) * Number(GUICtrlRead($Input2)) & @CRLF)
EndFunc ;==>Calcula

Func SelectControl()
    If WinActive($Form1) Then
        $cf = ControlGetFocus("My Window", "")
        Switch $cf
            Case "Edit1"
                ControlFocus($Form1, "", "Edit2")
            Case "Edit2"
                ControlFocus($Form1, "", "Button1")
            Case "Button1"
                Calcula();needed here if Enter pressed
        EndSwitch
    EndIf

EndFunc ;==>SelectControl
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...