Fokus auf Button

  • Hallo,
    ich suche eine Möglichkeit das wenn ich etwas in der Inputbox eingebe auch das wieder mit Enter ausgeben kann.

    [autoit]


    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Form1", 487, 82, 192, 124)
    $Input1 = GUICtrlCreateInput("Input1", 8, 32, 329, 21)
    $Button1 = GUICtrlCreateButton("Button1", 344, 32, 131, 25, $WS_GROUP)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit][autoit][/autoit][autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Button1
    MsgBox(0, "Ausgabe",GUICtrlRead($Input1))
    #cs
    Das soll gehen, in dem ich auf den Button Klicke oder Enter klicke
    Das wär zwar kein Problem mit Hotkeyset oder ifpressed, aber es kann sein
    das im Programm auch mal enter getrükt wird ohne das man die absicht hat
    button1 zu betätigen.
    Also es soll es erst ausführen wenn in Input1 einen Fokus gibt und dann
    Enter gedrückt wurde.
    Bestes Beispiel: Webbrowser wenn ich hier in der URL Zeile Enter drücke wird die URL aufgerufen die ich eingetippt habe
    Ich kann jedoch auch ohne Fokus auf die Inputbox enter drücken und die seite wird nicht aufgerufen
    ich hoffe ihr versteht das
    #ce
    EndSwitch
    WEnd

    [/autoit][autoit][/autoit][autoit][/autoit]
  • Kein Problem^^
    So z.B.:

    Spoiler anzeigen
    [autoit]

    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Form1", 487, 82, 192, 124)
    $Input1 = GUICtrlCreateInput("Input1", 8, 32, 329, 21)
    $Button1 = GUICtrlCreateButton("Button1", 344, 32, 131, 25, $BS_DEFPUSHBUTTON)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Button1
    MsgBox(0, "Ausgabe",GUICtrlRead($Input1))
    EndSwitch
    WEnd

    [/autoit]
    • Offizieller Beitrag

    Wenn ich es richtig verstanden habe, soll das Input ausgelesen werden, wenn der Fokus im Input ist und ENTER gedrückt wird.
    Dazu brauchst du einen Hook:

    Spoiler anzeigen
    [autoit]

    #include <WinAPI.au3>
    #include <WindowsConstants.au3>
    Opt("GUIOnEventMode", 1)
    OnAutoItExitRegister('OnAutoItExit')

    [/autoit] [autoit][/autoit] [autoit]

    Global $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
    Global $hmod = _WinAPI_GetModuleHandle(0)
    Global $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)
    Global $active = False, $gui, $input

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $gui = GUICreate('')
    GUISetOnEvent(-3, '_ende')
    $input = GUICtrlCreateInput('', 10,10,100)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState()

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    While 1
    If BitAND(WinGetState($gui), 8) Then
    $active = True
    Else
    $active = False
    EndIf
    Sleep(100)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _ende()
    Exit
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;===========================================================
    ; callback function
    ;===========================================================
    Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS, $vkCode, $ID, $index
    $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
    If $nCode < 0 Or Not $active Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    $vkCode = DllStructGetData($tKEYHOOKS, "vkCode")
    If $wParam = $WM_KEYDOWN And $vkCode = 13 Then ; ENTER gedrückt

    [/autoit] [autoit][/autoit] [autoit]

    $ID = _WinAPI_GetDlgCtrlID(ControlGetHandle($gui, '', ControlGetFocus($gui)))
    If $ID <> $input Then Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    ConsoleWrite(GUICtrlRead($input) & @CRLF) ; Hier darf auf keinen Fall eine MsgBox aufgerufen werden!

    [/autoit] [autoit][/autoit] [autoit]

    EndIf
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func OnAutoItExit()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hStub_KeyProc)
    EndFunc

    [/autoit]
  • Hier eine anfängerfreundliche Variante ohne gleich zu "hooken" ;)

    Spoiler anzeigen
    [autoit]


    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Form1", 487, 182, 192, 124)
    $Input1 = GUICtrlCreateInput("Input1", 8, 32, 329, 21)
    $Input2 = GUICtrlCreateInput("Input2", 8, 82, 329, 21)
    $Button1 = GUICtrlCreateButton("Button1", 344, 32, 131, 25, $BS_DEFPUSHBUTTON)
    $test = GUICtrlCreateButton("test", 344, 72, 131, 25)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    case $button1
    if _CheckFokus("Form1") then MsgBox(1, "", Guictrlread($Input1))
    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _CheckFokus($sGui)
    local $focus=ControlGetFocus($sGui)
    switch $focus
    case "Edit1"
    return true
    case "Button1"
    return true
    case else
    return false
    endswitch
    Endfunc

    [/autoit]
  • oder aber über WM_COMMAND:

    Spoiler anzeigen
    [autoit]

    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Form1", 487, 92, 192, 124)
    $Input1 = GUICtrlCreateInput("Input1", 8, 32, 329, 21)
    $Input1_Switch = False
    $Button1 = GUICtrlCreateButton("Button1", 344, 32, 131, 25)
    $Input2 = GUICtrlCreateInput("Input2", 8, 62, 329, 21)
    $Input2_Switch = False
    $Button2 = GUICtrlCreateButton("Button2", 344, 62, 131, 25)
    GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Button1
    MsgBox(0, "Ausgabe",GUICtrlRead($Input1))
    Case $Button2
    MsgBox(0, "Ausgabe",GUICtrlRead($Input2))
    EndSwitch

    [/autoit] [autoit][/autoit] [autoit]

    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    $nNotifyCode = BitShift($wParam, 16)
    $nID = BitAND($wParam, 0x0000FFFF)
    If $nNotifyCode = 256 Then
    Switch $nID
    Case $Input1
    GUICtrlSetStyle($Button2, '')
    GUICtrlSetState($Button1, $GUI_DEFBUTTON)
    Case $Input2
    GUICtrlSetStyle($Button1, '')
    GUICtrlSetState($Button2, $GUI_DEFBUTTON)
    EndSwitch
    EndIf
    EndFunc ;==>MY_WM_COMMAND

    [/autoit]

    MfG Schnuffel

    "Sarkasmus ist die niedrigste Form des Witzes, aber die höchste Form der Intelligenz."
    Val McDermid

    ein paar Infos ...

    Wer mehr als "nur" Hilfe benötigt, kann sich gern im Forum "Programmieranfragen" an uns wenden. Wir helfen in allen Fällen, die die Forenregeln zulassen.

    Für schnelle Hilfe benötigen wir ein ! lauffähiges ! Script, dass wir als Demonstration des Problems testen können. Wer von uns erwartet ein Teilscript erstmal lauffähig zu bekommen, der hat
    1. keine wirkliche Not
    2. keinen Respekt vor Menschen die ihm in ihrer Freizeit Ihre Hilfe anbieten
    3. oder ist einfach nur faul und meint wir coden das für ihn

    In solchen Fällen erlaube ich mir, die Anfrage einfach zu ignorieren. ;)

  • ... oder aber per GUIOnEventMode + WM_COMMAND + Hotkey:

    Spoiler anzeigen
    [autoit]

    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Opt("GUIOnEventMode", 1)
    $Form1 = GUICreate("Form1", 487, 121, 192, 124)
    GUISetOnEvent($GUI_EVENT_CLOSE, "GUI_Close")
    $Input1 = GUICtrlCreateInput("Input1", 8, 20, 329, 21)
    $Button1 = GUICtrlCreateButton("Button1", 344, 18, 131, 25)
    GUICtrlSetOnEvent(-1, "Button1_Clicked")
    $Input2 = GUICtrlCreateInput("Input2", 8, 50, 329, 21)
    $Button2 = GUICtrlCreateButton("Button2", 344, 48, 131, 25)
    GUICtrlSetOnEvent(-1, "Button2_Clicked")
    $Input3 = GUICtrlCreateInput("Input3", 8, 80, 329, 21)
    $Button3 = GUICtrlCreateButton("Button3", 344, 78, 131, 25)
    GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit]

    While True
    Sleep(100)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func GUI_Close()
    Exit
    EndFunc ;==>GUI_Close

    [/autoit] [autoit][/autoit] [autoit]

    Func Button1_Clicked()
    MsgBox(0, "Ausgabe", GUICtrlRead($Input1))
    EndFunc ;==>Button1_Clicked

    [/autoit] [autoit][/autoit] [autoit]

    Func Button2_Clicked()
    MsgBox(0, "Ausgabe", GUICtrlRead($Input2))
    EndFunc ;==>Button2_Clicked

    [/autoit] [autoit][/autoit] [autoit]

    Func MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Local Const $EN_KILLFOCUS = 0x200
    Local Const $EN_SETFOCUS = 0x100
    Local $nNotifyCode = BitShift($wParam, 16)
    Local $nID = BitAND($wParam, 0x0000FFFF)
    If $nID = $Input1 Or $nID = $Input2 Then
    Switch $nNotifyCode
    Case $EN_KILLFOCUS
    HotKeySet("{Enter}")
    ConsoleWrite("Hotkey: Aus" & @CRLF) ; <-- nur zum Testen
    Case $EN_SETFOCUS
    If $nID = $Input1 Then
    HotKeySet("{Enter}", "Button1_Clicked")
    ConsoleWrite("Hotkey: Button1" & @CRLF) ; <-- nur zum Testen
    Else
    HotKeySet("{Enter}", "Button2_Clicked")
    ConsoleWrite("Hotkey: Button2" & @CRLF) ; <-- nur zum Testen
    EndIf
    EndSwitch
    EndIf
    EndFunc ;==>MY_WM_COMMAND

    [/autoit]