Jump to content

Trigger input ONLY when hitting enter


Recommended Posts

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

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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

×
×
  • Create New...