corgano Posted May 23, 2015 Posted May 23, 2015 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 wendWhat 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
Moderators Melba23 Posted May 23, 2015 Moderators Posted May 23, 2015 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 WEndM23 DickG 1 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
gottygolly Posted May 23, 2015 Posted May 23, 2015 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
corgano Posted May 25, 2015 Author Posted May 25, 2015 (edited) 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 M23Dude, when did we get GUISetAccelerators()? That is exactly the behavior I was looking for! awesome . 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 May 25, 2015 by corgano 0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e
Moderators Melba23 Posted May 25, 2015 Moderators Posted May 25, 2015 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) ThenIf 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now