Jump to content
Sign in to follow this  
AndyS01

unable to handle focus events for controls

Recommended Posts

AndyS01

I have an AutoIT GUI that has 2 text controls and a button. When I click on the button, I want to display the text from the last of the 2 text input controls that had focus.

I couldn't find a way to capture on-focus events, so I handled all mouse left button events and looked to see if they were from one of the 2 text input controls, and if so, remember the control ID.

However, when I get the interrupt, the @GUI_CtrlId value is always -7

Here is an extract from my script:

#include <GuiConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)
Opt('MustDeclareVars', 1)
Opt("GUIEventOptions", 1)

Global $iID = 0, $hMainwin, $Btn, $Input1, $Input2

$hMainwin = GUICreate("main", 120, 100)

$Input1 = GUICtrlCreateInput("text 1", 10, 10, 100, 20)
$Input2 = GUICtrlCreateInput("text 2", 10, 30, 100, 20)
$Btn = GUICtrlCreateButton("Button", 10, 50, 100, 20)

ConsoleWrite("+++: $Input1 = " & $Input1 & @CRLF)
ConsoleWrite("+++: $Input2 = " & $Input2 & @CRLF)

GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "Event_Focus"); Focus started on a control
GUICtrlSetOnEvent($Btn, 'handle_btn')
GUISetState()

While 1
    Sleep(500) ; sleep a short while
WEnd

Func Event_Focus()
    Local $id = @GUI_CtrlId
    ConsoleWrite("+++: Event_Focus() called.  ID = " & $id & @CRLF)
    If (($id == $Input1) Or ($id == $Input2)) Then $iID = $id
EndFunc   ;==>Event_Focus

Func handle_btn()
    MsgBox(0, "INFO", "$iID = " & $iID & @CRLF & "text ==>" & GUICtrlRead($iID) & "<==")
    Exit (1)
EndFunc   ;==>handle_btn

Andy

Share this post


Link to post
Share on other sites
Melba23

AndySO1,

Here is one way to do it. You could faff around with EN_SETTFOCUS and EN_KILLFOCUS messages, but just looking for the last active input seems easier: :huggles:

#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

Opt("GUIOnEventMode", 1)
Opt('MustDeclareVars', 1)
Opt("GUIEventOptions", 1)

Global $iLast_Focus, $hInput_1, $hInput_2, $iActive

Global $iID = 0, $hMainwin, $Btn, $Input1, $Input2

$hMainwin = GUICreate("main", 120, 100)

$Input1 = GUICtrlCreateInput("text 1", 10, 10, 100, 20)
$hInput_1 = GUICtrlGetHandle(-1)
$Input2 = GUICtrlCreateInput("text 2", 10, 30, 100, 20)
$hInput_2 = GUICtrlGetHandle(-1)
$Btn = GUICtrlCreateButton("Button", 10, 50, 100, 20)

ConsoleWrite("+++: $Input1 = " & $Input1 & @CRLF)
ConsoleWrite("+++: $Input2 = " & $Input2 & @CRLF)

GUICtrlSetOnEvent($Btn, 'handle_btn')
GUISetState()

While 1
    Sleep(10) ; sleep a short while

    $iLast_Focus = _WinAPI_GetFocus()
    Switch $iLast_Focus
        Case $hInput_1
            $iActive = $Input1
        Case $hInput_2
            $iActive = $Input2
    EndSwitch

WEnd

Func handle_btn()

    MsgBox(0, "INFO", "$iID = " & $iActive & @CRLF & "text ==>" & GUICtrlRead($iActive) & "<==")

    Exit (1)
EndFunc   ;==>handle_btn

I hope this does what you want. :D

M23

Edit: SO1 = ?

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Melba23

AdmiralAlkex,

You are almost certainly correct - my old eyes you know..... :D

It is just that S(Oh)1 used to mean something in a past life. :huggles:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
PsaltyDS

I trapped WM_COMMAND and it seemed to only send EN_CHANGE. Was expecting EN_SETFOCUS and EN_KILLFOCUS, but never saw them. Settled on this since all he wanted was the last Input to send ANY message:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

Global $iID = 0, $hMainwin, $idBtn, $idInput1, $hInput1, $idInput2, $hInput2

$hMainwin = GUICreate("main", 120, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")

$idInput1 = GUICtrlCreateInput("text 1", 10, 10, 100, 20)
$hInput1 = ControlGetHandle($hMainwin, "", $idInput1)
$idInput2 = GUICtrlCreateInput("text 2", 10, 30, 100, 20)
$hInput2 = ControlGetHandle($hMainwin, "", $idInput2)
$idBtn = GUICtrlCreateButton("Button", 10, 50, 100, 20)
GUICtrlSetOnEvent(-1, 'handle_btn')
GUISetState()

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Sleep(10) ; sleep a short while
WEnd

Func _WM_COMMAND($hWnd, $iMsg, $WPARAM, $LPARAM)
    Local $WPARAM_Lo = BitAND($WPARAM, 0x0000FFFF)
    Switch $LPARAM
        Case $hInput1, $hInput2
            $iID = $WPARAM_Lo
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

Func handle_btn()
    ConsoleWrite("Button Hit:  $iID = " & $iID & "; text = " & GUICtrlRead($iID) & @LF)
EndFunc   ;==>handle_btn

Func _Quit()
    Exit
EndFunc   ;==>_Quit

:D


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
AndyS01

Thank you very much, folks, the suggested solutions worked perfectly!

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.