Jump to content
corgano

Trigger input ONLY when hitting enter

Recommended Posts

corgano

Assume a simple gui with one input. I have the usual code using a while loop:

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case -3
            Exit
        Case $Input
            consolewrite("Input triggered" & @CRLF)
    endswitch
wend

What I want to achieve is this. When a user enters text in the input ad presses enter, it triggers. However, if they enter or change the input and then click somewhere else, without hitting enter in only that input box, it will not trigger. What is the best way to do this?

My best idea so far is to ho a hotkeyset("{enter}", "inputhandler"), but then I need to check the window and keep unsetting / send enter / setting it if they hit enter in another window. Is there a better way?


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites
Melba23

corgano,

I use an Accelerator key to do this:

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("Test", 500, 500)

$cInput_1 = GUICtrlCreateInput("", 10, 10, 100,20)
$cInput_2 = GUICtrlCreateInput("", 10, 50, 100,20)

$cDummy = GUICtrlCreateDummy()

GUISetState(@SW_SHOW)

; Set accelerator for Enter to action the Dummy code
Dim $AccelKeys[1][2]=[["{ENTER}", $cDummy]]
GUISetAccelerators($AccelKeys)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $cDummy
            ; Check input has focus
            If _WinAPI_GetFocus() = GUICtrlGetHandle($cInput_1) Then
                $sText = GUICtrlRead($cInput_1)
                MsgBox(0,"test", "You typed " & $sText & " in Input 1")
            EndIf
    EndSwitch
WEnd

M23

  • Like 1

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
gottygolly

Probably the best way to do it is with a guictrlsetonevent.

$gui = GuiCreate("",200,200,-1,-1)
Opt("GUIOnEventMode",1)
GuiSetOnEvent(-3,"_Exit")
$input_1 = GUICtrlCreateInput("Field 1",5,5,100,20)
$input_2 = GUICtrlCreateInput("Field 2",5,40,100,20)
GUICtrlSetOnEvent($input_1,"_Input_1")
GUICtrlSetOnEvent($input_2,"_Input_2")
GuiSetState()

While 1
Sleep(10)
Wend

Func _Exit()
Exit
EndFunc

Func _Input_1()
    MsgBox(0,"Field 1","Field one (input 1) has been read!"&@crlf&"Read: "&guictrlread($input_1))
EndFunc

Func _Input_2()
    MsgBox(0,"Field 2","Field two (input 2) has been read!"&@crlf&"Read: "&guictrlread($input_2))
EndFunc

 

Share this post


Link to post
Share on other sites
corgano

Probably the best way to do it is with a guictrlsetonevent.

 

$gui = GuiCreate("",200,200,-1,-1)
Opt("GUIOnEventMode",1)
GuiSetOnEvent(-3,"_Exit")
$input_1 = GUICtrlCreateInput("Field 1",5,5,100,20)
$input_2 = GUICtrlCreateInput("Field 2",5,40,100,20)
GUICtrlSetOnEvent($input_1,"_Input_1")
GUICtrlSetOnEvent($input_2,"_Input_2")
GuiSetState()

While 1
Sleep(10)
Wend

Func _Exit()
Exit
EndFunc

Func _Input_1()
    MsgBox(0,"Field 1","Field one (input 1) has been read!"&@crlf&"Read: "&guictrlread($input_1))
EndFunc

Func _Input_2()
    MsgBox(0,"Field 2","Field two (input 2) has been read!"&@crlf&"Read: "&guictrlread($input_2))
EndFunc

 

​That doesn't answer the question, because both boxes still fire events on text change, and not only when enter is hit. I personally prefer using a loop, although being honest I don't know why.

corgano,

I use an Accelerator key to do this:

 

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("Test", 500, 500)

$cInput_1 = GUICtrlCreateInput("", 10, 10, 100,20)
$cInput_2 = GUICtrlCreateInput("", 10, 50, 100,20)

$cDummy = GUICtrlCreateDummy()

GUISetState(@SW_SHOW)

; Set accelerator for Enter to action the Dummy code
Dim $AccelKeys[1][2]=[["{ENTER}", $cDummy]]
GUISetAccelerators($AccelKeys)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $cDummy
            ; Check input has focus
            If _WinAPI_GetFocus() = GUICtrlGetHandle($cInput_1) Then
                $sText = GUICtrlRead($cInput_1)
                MsgBox(0,"test", "You typed " & $sText & " in Input 1")
            EndIf
    EndSwitch
WEnd

 

M23

​Dude, when did we get GUISetAccelerators()? That is exactly the behavior I was looking for! awesome :D. Question though, how come it works for input1, but not for input2? How do I tell it which input(s) I want to be triggered by {enter}?

Assume I wanted input2 to be activated by enter and input1 not to trigger any event, how would you change the example to reflect this?

Edit: I'm either an idiot or half asleep. not sure which yet. Just realized why. Thanks for the example!

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites
Melba23

corgano,

when did we get GUISetAccelerators()?

 From the Help file changelog: 3.2.12.0 (16th May, 2008) (Release) - Added: GUISetAccelerators(). So not exactly new.....

how come it works for input1, but not for input2? How do I tell it which input(s) I want to be triggered by {enter}?

 I would have thought that this section of the code would have given a clue:

; Check input has focus
    If _WinAPI_GetFocus() = GUICtrlGetHandle($cInput_1) Then

If you want other controls reacting to the Accelerator then you just need to check if they have focus.

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

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

  • Similar Content

    • Reziskonh
      By Reziskonh
      Hi, everybody
      I look for GUI whose behavior similar to the message of an email client or antivirus
      In other words:
          Any PC screen able to calculate the size
          To consider the Task bar size
          To find the lower corner on the right/below and to nestle on it

      Notes:
      In GUI the GUICtrlCreateEdit field (as option) - that can be received and displayed through variable information
      Thanks a lot
      PS
      I use the translator, excuse if something is not clear
       

    • VollachR
      By VollachR
      Hi,
      I'd like to show a progress bar for an operation performed by an external program my script is running silently, I want to show it in a GUI I created using the GUICtrlCreateProgress but I have no idea how to do it.
      The important thing to point out is that there's no way of knowing how long the external program will run, as it is a file splitter and it depends on the size of file it splits and the split parts size.
      Can someone point me in the right direction or give me an example how to do so?
      This is my RunWait command:
      RunWait($MYFILES1 & '\fsplit.exe -split ' & $Size & ' mb ' & $File & " -f " & $File & "." & $extension) It uses multiple variables declared and set earlier in the script, how will I got about having the progress of that command shown using GUICtrlCreateProgress ? Is it possible?
      Thank you.
    • nacerbaaziz
      By nacerbaaziz
      Hello my friends
      I have an inquiry and I hope to find the answer here
      I want to create a graphical user interface
      but I want to hide the system menu
      I mean the window menu
      Is this possible?
      If is possible please give me how to do that
      Thanks in advance
    • helmar
      By helmar
      I was playing around with simple GUI creation.  I tend to like parameter driven coding (in prior life (years ago) as Clipper/FoxPro/dBase coder).  Just wanted to see what I could do with a GUI.
      #Region options, includes Opt('MustDeclareVars', 1) Opt("GUIOnEventMode", 1) ; Change to OnEvent mode #include <GUIConstantsEx.au3> #EndRegion options, includes Global Const $nCols = 2, $nRows = 6, $nSpacer = 10, $nBtnWidth = 150, $nBtnHeight = 30 Global $xName = 0, $xID = 0 Global $nGUIWidth = ($nCols * $nBtnWidth) + (($nCols + 1) * $nSpacer) Global $nGUIHeight = ($nRows * $nBtnHeight) + (($nRows + 1) * $nSpacer) Global $hMainGUI = GUICreate("Calculated GUI", $nGUIWidth, $nGUIHeight, -1, -1) For $xRows = 1 to $nRows ;in this arrangement, tabbing is left to right, then next row For $xCols = 1 to $nCols ;reverse the order of this line with the prior line for top to bottom tabbing, then next col $xName += 1 $xID += 1 Global $Dummy = GUICtrlCreateButton("Button" & $xName, _ ($nBtnWidth * ($xCols - 1)) + (($xCols - 1) * $nSpacer) + $nSpacer, _ ($nBtnHeight* ($xRows - 1)) + (($xRows - 1) * $nSpacer) + $nSpacer, _ $nBtnWidth, _ $nBtnHeight) GUICtrlSetOnEvent($xID+2, "Handler") Next Next GUISetOnEvent($GUI_EVENT_CLOSE, "CloseApp") GUISetState(@SW_SHOW) While 1 Sleep(10) WEnd Func CloseApp() Exit EndFunc Func Handler() MsgBox(0,0,"Button " & @GUI_CtrlId - 2) EndFunc  
    • XinYoung
      By XinYoung
      For fun, I'm building an app that opens a webpage and refreshes it every 30 seconds.
      But once the script performs _IEAction($oIE, "refresh"), the GUI closes.
      Any help is appreciated.
       

      #include <ButtonConstants.au3>
      #include <EditConstants.au3>
      #include <GUIConstantsEx.au3>
      #include <StaticConstants.au3>
      #include <WindowsConstants.au3>
      #include <WinAPIFiles.au3>
      #include <Array.au3>
      #include <File.au3>
      #include <Excel.au3>
      #include <DateTimeConstants.au3>
      #include <MsgBoxConstants.au3>
      #include <WinAPIShellEx.au3>
      #include <MsgBoxConstants.au3>
      #include <Date.au3>
      #include <ComboConstants.au3>
      #include <guimenu.au3>
      #include <IE.au3>
      HotKeySet("{F4}", "_Exit")
      ;Open the file(s) in the selected folder
      $extension = ".txt"
      $app2openWith = @SystemDir & "\notepad.exe"
      Func Begin()
          Global $loopTrick = 0
          #Region ### START Koda GUI section ### Form=c:\users\mchu\downloads\autoit\my code\form1.kxf
          Global $UI = GUICreate("Hit Em Up!", 256, 113, -1, -1)
          GUISetBkColor(0x000000)
          $menu = _GUICtrlMenu_GetSystemMenu($UI)
          _GUICtrlMenu_EnableMenuItem($menu, $SC_CLOSE, 1, False)
          Global $url = GUICtrlCreateInput("https://www.youtube.com/watch?v=dQw4w9WgXcQ", 81, 8, 160, 21)
          $Label1 = GUICtrlCreateLabel("Target:", 16, 8, 55, 17)
          GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
          GUICtrlSetColor(-1, 0x00FF00)
          Global $StartBut = GUICtrlCreateButton("Start", 16, 40, 67, 25)
          GUICtrlSetFont(-1, 13, 800, 0, "MS Sans Serif")
          GUICtrlSetBkColor(-1, 0x008000)
          $Label2 = GUICtrlCreateLabel("(Press F4 to Exit)", 96, 40, 8000, 17)
          GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
          GUICtrlSetColor(-1, 0x00FF00)
          GUISetState(@SW_SHOW)
          #EndRegion ### END Koda GUI section ###
          While 1
              $UIfunc = GUIGetMsg()
              Select
                  Case $UIfunc = $GUI_EVENT_CLOSE
                      _Exit()
                  Case $UIfunc = $StartBut
                      If GUICtrlRead($url) = "" Then
                          MsgBox(48, "Um...", "Give me a target you idiot.")
                      Else
                          GUICtrlSetState($url, $GUI_DISABLE)
                          GUICtrlSetState($StartBut, $GUI_DISABLE)
                          Start()
                      EndIf
              EndSelect
          WEnd
      EndFunc   ;==>Begin
      Func Start()
          If $loopTrick = 0 Then
              Global $oIE = _IECreate(GUICtrlRead($url))
              _IELoadWait($oIE)
              Again()
          ElseIf $loopTrick = 1 Then
              Sleep(3000)
              _IEAction($oIE, "refresh")
              Sleep(3000)
              Start()
          EndIf
      EndFunc   ;==>Start
      Func Again()
          $loopTrick = 1
          Start()
      EndFunc   ;==>Again
      Func _Exit()
          Exit
      EndFunc   ;==>_Exit
×