Jump to content

ControlGetFocus() help


Recommended Posts

Hello. My intent is to have a program do a thing if a cursor is flashing in a search field. My trouble is that I haven't been able to get my head around ControlGetFocus(), if that is even the proper way to do this. This is the basic idea of what I've been attempting, and it's not at all working.
 

Global $Search_Field = GUICtrlCreateInput("", 208,20,90,20)

While ControlGetFocus($GUI_FUNDAMENTAL_WINDOW) = $Search_Field
      MsgBox($MB_OK, "Cursor in text field", "Cursor in text field")
WEnd

Is what I'm attempting a valid concept? Will someone please tell me how I have been silly? I can post more of the code if it is relevant.

Link to comment
Share on other sites

ControlGetFocus (...) returns if success: the ClassNameNN of the control that has keyboard focus within a specified window (string).

$Search_Field contains the Control ID of the input field (number).

Comparing string with number will not work ...

Link to comment
Share on other sites

Basic example, using two inputs:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>

Global $g_idSearch1, $g_idSearch2

_Example()

Func _Example()
    Local $hControlFocus
    GUICreate("Example", 260, 55)
    GUICtrlCreateLabel("Search1:", 5, 5, 60, 20, $SS_CENTERIMAGE)
        GUICtrlSetFont(-1, 11, 800)
    $g_idSearch1 = GUICtrlCreateInput("", 75, 5, 120, 20)
    Local $idSearch1 = GUICtrlCreateButton("Go", 195, 5, 60, 20)
        GUICtrlSetFont(-1, 11, 800)
    GUICtrlCreateLabel("Search2:", 5, 30, 60, 20, $SS_CENTERIMAGE)
        GUICtrlSetFont(-1, 11, 800)
    $g_idSearch2 = GUICtrlCreateInput("", 75, 30, 120, 20)
    Local $idSearch2 = GUICtrlCreateButton("Go", 195, 30, 60, 20)
        GUICtrlSetFont(-1, 11, 800)
    Local $idEnter = GUICtrlCreateDummy()
    GUISetState()
    Local $aAccelKeys[1][2] = [["{ENTER}", $idEnter]]
    GUISetAccelerators($aAccelKeys)
    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $idSearch1
                MsgBox(4096, "Search 1", "Search 1 : " & GUICtrlRead($g_idSearch1))
            Case $idSearch2
                MsgBox(4096, "Search 2", "Search 2 : " & GUICtrlRead($g_idSearch2))
            Case $idEnter ;~ Check if Enter is pressed
                $hControlFocus = _WinAPI_GetFocus()
                Switch $hControlFocus
                    Case GUICtrlGetHandle($g_idSearch1)
                        MsgBox(4096, "Search 1", "Search 1 : " & GUICtrlRead($g_idSearch1))
                    Case GUICtrlGetHandle($g_idSearch2)
                        MsgBox(4096, "Search 2", "Search 2 : " & GUICtrlRead($g_idSearch2))
                EndSwitch
        EndSwitch
    WEnd
EndFunc

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode
    $hWndFrom = $lParam
    $iIDFrom = BitAND($wParam, 0xFFFF)
    $iCode = BitShift($wParam, 16)
    Switch $iIDFrom
        Case $g_idSearch1
            Switch $iCode
                Case $EN_CHANGE ; Sent when the user has taken an action that may have altered text in an edit control
                    ConsoleWrite("Search Item 1: " & GUICtrlRead($g_idSearch1) & @CRLF)
            EndSwitch
        Case $g_idSearch2
            Switch $iCode
                Case $EN_CHANGE ; Sent when the user has taken an action that may have altered text in an edit control
                    ConsoleWrite("Search Item 2: " & GUICtrlRead($g_idSearch2) & @CRLF)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

 

Link to comment
Share on other sites

Subz, you're still beautiful, but it took me a few minutes to realize that your example is not what I was intending as I am full of whiskey.

 

I intend to have the action of clicking in the input box with the intent to type trigger an action.

 

In broad strokes, I have a bunch of radio buttons which govern which child GUI embedded in the parent is visible. Selecting one deletes all other child GUIs and displays the selected one. My intent is to have clicking in the search field deselect all radio buttons and embed a GUI specific to the search field.

Link to comment
Share on other sites

Not really sure, but you could use something like:

#include <Array.au3>
#include <EditConstants.au3>
#include <GuiComboBox.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>

Global $g_idSearch, $g_idRadio[5][2] = [["", "Radio1"], ["", "Radio2"], ["", "Radio3"], ["", "Radio4"], ["", "Radio5"]]

_Example()

Func _Example()
    Local $hControlFocus, $y = 30
    GUICreate("Example", 260, (UBound($g_idRadio) * 30 ) + 5)
    GUICtrlCreateLabel("Search1:", 5, 5, 60, 20, $SS_CENTERIMAGE)
        GUICtrlSetFont(-1, 11, 800)
    $g_idSearch = GUICtrlCreateCombo("", 75, 5, 120, 20)
    _GUICtrlComboBox_BeginUpdate($g_idSearch)
    For $i = 0 To UBound($g_idRadio) - 1
        _GUICtrlComboBox_AddString($g_idSearch, $g_idRadio[$i][1])
        $g_idRadio[$i][0] = GUICtrlCreateRadio($g_idRadio[$i][1], 5, $y, 120, 20)
        $y += 25
    Next
    _GUICtrlComboBox_EndUpdate($g_idSearch)
    Local $idEnter = GUICtrlCreateDummy()
    GUISetState()
    Local $aAccelKeys[1][2] = [["{ENTER}", $idEnter]]
    GUISetAccelerators($aAccelKeys)
    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $idEnter ;~ Check if Enter is pressed
                $hControlFocus = _WinAPI_GetFocus()
                Switch $hControlFocus
                    Case GUICtrlGetHandle($g_idSearch)
                        MsgBox(4096, "Search 1", "Search 1 : " & GUICtrlRead($g_idSearch))
                EndSwitch
        EndSwitch
    WEnd
EndFunc

Func _Edit_Changed()
    _GUICtrlComboBox_AutoComplete($g_idSearch)
EndFunc   ;==>_Edit_Changed

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode, $hWndCombo
    If Not IsHWnd($g_idSearch) Then $hWndCombo = GUICtrlGetHandle($g_idSearch)
    $hWndFrom = $lParam
    $iIDFrom = BitAND($wParam, 0xFFFF) ; Low Word
    $iCode = BitShift($wParam, 16) ; Hi Word
    Switch $hWndFrom
        Case $g_idSearch, $hWndCombo
            Switch $iCode
                Case $CBN_CLOSEUP ; Sent when the list box of a combo box has been closed
                    _DebugPrint("$CBN_CLOSEUP" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_DBLCLK ; Sent when the user double-clicks a string in the list box of a combo box
                    _DebugPrint("$CBN_DBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_DROPDOWN ; Sent when the list box of a combo box is about to be made visible
                    _DebugPrint("$CBN_DROPDOWN" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_EDITCHANGE ; Sent after the user has taken an action that may have altered the text in the edit control portion of a combo box
                    _DebugPrint("$CBN_EDITCHANGE" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    _Edit_Changed()
                    ; no return value
                Case $CBN_EDITUPDATE ; Sent when the edit control portion of a combo box is about to display altered text
                    _DebugPrint("$CBN_EDITUPDATE" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_ERRSPACE ; Sent when a combo box cannot allocate enough memory to meet a specific request
                    _DebugPrint("$CBN_ERRSPACE" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_KILLFOCUS ; Sent when a combo box loses the keyboard focus
                    Local $iSearch = _ArraySearch($g_idRadio, GUICtrlRead($g_idSearch), 0, 0, 0, 0, 1, 1)
                    If $iSearch > -1 Then GUICtrlSetState($g_idRadio[$iSearch][0], $GUI_CHECKED)
                    _DebugPrint("$CBN_KILLFOCUS" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_SELCHANGE ; Sent when the user changes the current selection in the list box of a combo box
                    Local $iSearch = _ArraySearch($g_idRadio, GUICtrlRead($g_idSearch), 0, 0, 0, 0, 1, 1)
                    If $iSearch > -1 Then GUICtrlSetState($g_idRadio[$iSearch][0], $GUI_CHECKED)
                    _DebugPrint("$CBN_SELCHANGE" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_SELENDCANCEL ; Sent when the user selects an item, but then selects another control or closes the dialog box
                    _DebugPrint("$CBN_SELENDCANCEL" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_SELENDOK ; Sent when the user selects a list item, or selects an item and then closes the list
                    _DebugPrint("$CBN_SELENDOK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
                Case $CBN_SETFOCUS ; Sent when a combo box receives the keyboard focus
                    For $i = 0 To UBound($g_idRadio) - 1
                        GUICtrlSetState($g_idRadio[$i][0], $GUI_UNCHECKED)
                    Next
                    _DebugPrint("$CBN_SETFOCUS" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
                            "-->Code:" & @TAB & $iCode)
                    ; no return value
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func _DebugPrint($s_Text, $sLine = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @CRLF & _
            "+======================================================" & @CRLF & _
            "-->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text & @CRLF & _
            "+======================================================" & @CRLF)
EndFunc   ;==>_DebugPrint

 

Link to comment
Share on other sites

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...