tammelinn Posted November 11, 2007 Posted November 11, 2007 (edited) I have a simple script, with an input control where the user can type commands. Func SetupMainOutputWindow() ; Open the main output window in its initial position ; Change to OnEvent mode Opt("GUIOnEventMode", 1) ; Open the window $mainoutputhandle = GUICreate( ($scriptname & " v" & $scriptversion), ($mainwindowwidth), ($mainwindowheight-50), 0, 0) GUISetState(@SW_SHOW) ; Set the window to be closeable GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") ; Create black box of white text to show the script's output $mainoutputtext = GUICtrlCreateEdit ("", 0, 0, ($mainwindowwidth-200), ($mainwindowheight-120), $ES_READONLY) GUICtrlSetColor($mainoutputtext, 0xFFFFFF) GUICtrlSetBkColor($mainoutputtext, 0x000000) [color="#008000"]; Create a smaller box for the user to type commands $userinputlabel = GUICtrlCreateLabel ("Command entry", 10, ($mainwindowheight-110), ($mainwindowwidth-220), 20, $ES_READONLY) $userinputbox = GUICtrlCreateInput ("", 10, ($mainwindowheight-90), ($mainwindowwidth-220), 20) GUICtrlSetOnEvent($userinputbox, "ParseInput")[/color] ; Show the time $display[1][1] = GUICtrlCreateInput ( _DateTimeFormat( _NowCalc(),2), ($mainwindowwidth - 200 + 20), 0, 70, 20, $ES_READONLY) $display[1][2] = GUICtrlCreateLabel ( "Date", ($mainwindowwidth - 200 + 20), 20, 70, 15, $ES_READONLY) $display[2][1] = GUICtrlCreateInput ( _DateTimeFormat( _NowCalc(),5), ($mainwindowwidth - 200 + 100), 0, 70, 20, $ES_READONLY) $display[2][2] = GUICtrlCreateLabel ( "Time", ($mainwindowwidth - 200 + 100), 20, 70, 15, $ES_READONLY) End Func Every 5 seconds or so, the main loop updates the time: Func UpdateDisplay() ; Updates the time GUICtrlSetData ($display[1][1], _DateTimeFormat( _NowCalc(),2)) GUICtrlSetData ($display[2][1], _DateTimeFormat( _NowCalc(),5)) EndFunc I'd like the function ParseInput() to respond to what the user types, whenever the user presses their <enter> key. Func ParseInput() ; Deal with user input to the command box Local $inputtext ; Set $inputtext to what was typed in the box $inputtext = GuiCtrlRead($userinputbox) MsgBox("box received " & $inputtext) EndFunc Here is my problem. Whenever the date is updated by UpdateDisplay() - it happens every five seconds - ParseInput() is also called. This is very inconvenient when the user hasn't finished typing their command. Instead of receiving the command "newtask", ParseInput() receives something useless like "newt" or "new". Could anyone suggest a way I can stop this from happening? Edited November 11, 2007 by tammelinn
Nahuel Posted November 11, 2007 Posted November 11, 2007 (edited) Your problem, is that your calling a function by an event in an inputbox. So, it will be called when you press enter, when you type something and then click outside the window, when another control steals focus and there's new text entered, etc... So, an idea would be to create a hidden button with the style $BS_DEFPUSHBUTTON. This way, the button will be pressed when you hit enter calling ParseInput(). Here's an example: #include <GUIConstants.au3> $Form1 = GUICreate("", 412, 74, 193, 135) $command = GUICtrlCreateInput("", 50, 32, 307, 21) $Label1 = GUICtrlCreateLabel("Type command", 50, 12, 77, 17) $Button1 = GUICtrlCreateButton("", 374, 28, 29, 29, $BS_DEFPUSHBUTTON) GUICtrlSetState(-1, $GUI_HIDE) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 MsgBox(0,"Command is:",GUICtrlRead($command)) ControlFocus($Form1,"",$command);This isn't really necessary, but just in case... EndSwitch WEnd I was trying to do it on yours but it has many undeclared variables and I'm feeling kinda lazy today... Edited November 11, 2007 by Nahuel
tammelinn Posted November 11, 2007 Author Posted November 11, 2007 Many thanks for your excellent explanation. Your code uses a loop that repeats every 100ms. I have a problem, because my script's main loop repeats after a minimum of 5 seconds, and sometimes much longer. My script already has a pause mode, so I thought it would be sensible to tell the user to pause the script before typing a command. In that way, I can use your 100ms loop. I tried to integrate your code into my script but I couldn't make it work. I suspect that my hidden button is in the wrong place; I don't understand the documentation on $BS_DEFPUSHBUTTON. Am I doing something obviously wrong? Here's a reproduction script. expandcollapse popup#include <GUIConstants.au3> #include <Date.au3> #include <GuiEdit.au3> ; Pause the script with this hotkey HotKeySet("{F1}", "PauseScript") ; Declare variables ; ----------------- ; The main output window's format Const $mainwindowwidth = 700 Const $mainwindowheight = 450 ; The handle of the GUI window Global $mainoutputhandle ; The name of the edit control where messages will appear Global $mainoutputtext ; The name of the input control where user can type commands Global $userinputlabel, $userinputbox ; The hidden button used in conjunction with this edit control Global $hiddenbutton ; Minimum period between each loop of the script in milliseconds Const $loopdelay = 5000 ; Keep track of the time Global $timestamp ; Allow the script to be paused Global $pausedflag ; Local variables Local $count ; Create the window ; ----------------- ; Open the main output window in its initial position of top-left corner ; Change to OnEvent mode Opt("GUIOnEventMode", 1) ; Open the window $mainoutputhandle = GUICreate("Test script", ($mainwindowwidth), ($mainwindowheight-50), 0, 0) GUISetState(@SW_SHOW) ; Create black box of white text in the window $mainoutputtext = GUICtrlCreateEdit ("", 0, 0, ($mainwindowwidth-200), ($mainwindowheight-120), $ES_READONLY) GUICtrlSetColor($mainoutputtext, 0xFFFFFF) GUICtrlSetBkColor($mainoutputtext, 0x000000) ; Create a smaller box for the user to type commands $userinputlabel = GUICtrlCreateLabel ("Pause the script with F1 before typing a command.", 10, ($mainwindowheight-110), ($mainwindowwidth-220), 20, $ES_READONLY) $userinputbox = GUICtrlCreateInput ("", 10, ($mainwindowheight-90), ($mainwindowwidth-220), 20) $hiddenbutton = GUICtrlCreateButton("", 374, 28, 29, 29, $BS_DEFPUSHBUTTON) GUICtrlSetState(-1, $GUI_HIDE) ; Main loop ; --------- ; Set the timer $timestamp = TimerInit() ; Every second, write a line of text to edit control Do Sleep($loopdelay) ; Write a line of text in the edit control GUICtrlSetData ($mainoutputtext, "Test line " & TimerDiff($timestamp) & @CRLF & GUICtrlRead ($mainoutputtext), "") Until TimerDiff($timestamp) >= 30000 ; Terminate the script after 30 seconds Exit 0 ; Pause script function ; --------------------- Func PauseScript() ; Pause the script, halting everything until the same hotkey is pressed, ; thereby allowing the user to type commands Local $nMsg ; Pause or unpause the script $pausedflag = NOT $pausedflag ; Write a line of text in the edit control If $pausedflag Then GUICtrlSetData ($mainoutputtext, "Script paused." & @CRLF & GUICtrlRead ($mainoutputtext), "") GUICtrlSetData ($userinputlabel, "Enter a command.") Else GUICtrlSetData ($mainoutputtext, "Script unpaused." & @CRLF & GUICtrlRead ($mainoutputtext), "") GUICtrlSetData ($userinputlabel, "Pause the script with F1 before typing a command.") EndIf While $pausedflag ; Allow the user to type information into the command box when the script is paused, ; and when the user presses return, call ParseInput() $nMsg = GUIGetMsg() Switch $nMsg Case $hiddenbutton ParseInput() Case $GUI_EVENT_CLOSE Exit EndSwitch Sleep(100) WEnd EndFunc ; PauseScript Func ParseInput() ; For this demonstration, the function simply writes the typed command onto the black screen Local $text $text = GUICtrlRead($userinputbox) GUICtrlSetData ($mainoutputtext, $text & @CRLF & GUICtrlRead ($mainoutputtext), "") EndFunc ; ParseInput
tammelinn Posted November 12, 2007 Author Posted November 12, 2007 Aha! And, what's more, Nahuel's line... ControlFocus($Form1,"",$command);This isn't really necessary, but just in case...oÝ÷ Ú+rÛ¯z¼§yǬ±ªòZ+a¢ëb¶Ø^nëm¢z',(®K(Ç©ä²ÞjwZQÞ謶¢ºÞr×±Êâ¦Ø§q«j|¨ì(ºW_wb¶ë~ébØ^~ën殶sb6æ6ÇVFRfÇC´uT6öç7FçG2æS2fwC°¢6æ6ÇVFRfÇC´FFRæS2fwC°¢6æ6ÇVFRfÇC´wVVFBæS2fwC° £²W6RFR67&BvFF2÷F¶W¤÷D¶W6WBgV÷C·´cÒgV÷C²ÂgV÷CµW6U67&BgV÷C² £²FV6Æ&Rf&&ÆW0£²ÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÐ £²FRÖâ÷WGWBvæF÷rb33·2f÷&Ö@¤6öç7Bb33c¶ÖçvæF÷wvGFÒs¤6öç7Bb33c¶ÖçvæF÷vVvBÒCS£²FRæFÆRöbFRuTvæF÷p¤vÆö&Âb33c¶Öæ÷WGWFæFÆP£²FRæÖRöbFRVFB6öçG&öÂvW&RÖW76vW2vÆÂV ¤vÆö&Âb33c¶Öæ÷WGWGFW@£²FRæÖRöbFRçWB6öçG&öÂvW&RW6W"6âGR6öÖÖæG0¤vÆö&Âb33c·W6W&çWFÆ&VÂÂb33c·W6W&çWF&÷£²FRFFVâ'WGFöâW6VBâ6öæ§Væ7FöâvFF2VFB6öçG&öÀ¤vÆö&Âb33c¶FFVæ'WGFöà £²Öæ×VÒW&öB&WGvVVâV6Æö÷öbFR67&BâÖÆÆ6V6öæG0¤6öç7Bb33c¶Æö÷FVÆÒS £²¶VWG&6²öbFRFÖP¤vÆö&Âb33c·FÖW7F×£²ÆÆ÷rFR67&BFò&RW6V@¤vÆö&Âb33c·W6VFfÆp £²Æö6Âf&&ÆW0¤Æö6Âb33c¶6÷Vç@ £²7&VFRFRvæF÷p£²ÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÐ £²÷VâFRÖâ÷WGWBvæF÷râG2æFÂ÷6FöâöbF÷ÖÆVgB6÷&æW £²÷VâFRvæF÷r¢b33c¶Öæ÷WGWFæFÆRÒuT7&VFRgV÷CµFW7B67&BgV÷C²Âb33c¶ÖçvæF÷wvGFÂb33c¶ÖçvæF÷vVvBÓS¤uT6WE7FFR5uõ4õr £²7&VFR&Æ6²&÷öbvFRFWBâFRvæF÷p¢b33c¶Öæ÷WGWGFWBÒuT7G&Ä7&VFTVFBgV÷C²gV÷C²ÂÂÂb33c¶ÖçvæF÷wvGFÓ#Âb33c¶ÖçvæF÷vVvBÓ#Âb33c´U5õ$TDôäŤuT7G&Å6WD6öÆ÷"b33c¶Öæ÷WGWGFWBÂdddddb¤uT7G&Å6WD&´6öÆ÷"b33c¶Öæ÷WGWGFWB £²7&VFR6ÖÆÆW"&÷f÷"FRW6W"FòGR6öÖÖæG0¢b33c·W6W&çWFÆ&VÂÒuT7G&Ä7&VFTÆ&VÂgV÷CµW6RFR67&BvFc&Vf÷&RGær6öÖÖæBâgV÷C²ÂÂb33c¶ÖçvæF÷vVvBÓÂb33c¶ÖçvæF÷wvGFÓ##Â#Âb33c´U5õ$TDôäÅ¢b33c·W6W&çWF&÷ÒuT7G&Ä7&VFTçWBgV÷C²gV÷C²ÂÂb33c¶ÖçvæF÷vVvBÓÂb33c¶ÖçvæF÷wvGFÓ##Â#¢b33c¶FFVæ'WGFöâÒuT7G&Ä7&VFT'WGFöâgV÷C²gV÷C²Â3sBÂ#Â#Â#Âb33c´%5ôDTeU4%UEDôâ¤uT7G&Å6WE7FFRÓÂb33c´uTôDR £²ÖâÆö÷£²ÒÒÒÒÒÒÒÒÐ £²6WBFRFÖW ¢b33c·FÖW7F×ÒFÖW$æB £²WfW'6V6öæBÂw&FRÆæRöbFWBFòVFB6öçG&öÀ ¤Fð 6ÆVWb33c¶Æö÷FVÆ ²w&FRÆæRöbFWBâFRVFB6öçG&öÀ uT7G&Å6WDFFb33c¶Öæ÷WGWGFWBÂgV÷CµFW7BÆæRgV÷C²fײFÖW$Ffbb33c·FÖW7F×fײ5$ÄbfײuT7G&Å&VBb33c¶Öæ÷WGWGFWBÂgV÷C²gV÷C² ¥VçFÂFÖW$Ffbb33c·FÖW7F×fwC³Ò3 £²FW&ÖæFRFR67&BgFW"36V6öæG0¤WB £²W6R67&BgVæ7FöࣲÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÒÐ ¤gVæ2W6U67&B ²W6RFR67&BÂÇFærWfW'FærVçFÂFR6ÖR÷F¶W2&W76VBÀ ²FW&V'ÆÆ÷værFRW6W"FòGR6öÖÖæG0 Æö6Âb33c¶ä×6p ²W6R÷"VçW6RFR67&@¢b33c·W6VFfÆrÒäõBb33c·W6VFfÆp ²w&FRÆæRöbFWBâFRVFB6öçG&öÀ bb33c·W6VFfÆrFVâ uT7G&Å6WDFFb33c¶Öæ÷WGWGFWBÂgV÷Cµ67&BW6VBâgV÷C²fײ5$ÄbfײuT7G&Å&VBb33c¶Öæ÷WGWGFWBÂgV÷C²gV÷C² uT7G&Å6WDFFb33c·W6W&çWFÆ&VÂÂgV÷C´VçFW"6öÖÖæBcFò&W7VÖRFR67&BâgV÷C² VÇ6P uT7G&Å6WDFFb33c¶Öæ÷WGWGFWBÂgV÷Cµ67&BVçW6VBâgV÷C²fײ5$ÄbfײuT7G&Å&VBb33c¶Öæ÷WGWGFWBÂgV÷C²gV÷C² uT7G&Å6WDFFb33c·W6W&çWFÆ&VÂÂgV÷CµW6RFR67&BvFc&Vf÷&RGær6öÖÖæBâgV÷C² VæD` vÆRb33c·W6VFfÆr ²ÆÆ÷rFRW6W"FòGRæf÷&ÖFöâçFòFR6öÖÖæB&÷vVâFR67&B2W6VBÀ ²æBvVâFRW6W"&W76W2&WGW&âÂ6ÆÂ'6TçWB b33c¶ä×6rÒuTvWD×6r 7vF6b33c¶ä×6p 66Rb33c¶FFVæ'WGFöà '6TçWB 66Rb33c´uTôUdTåEô4Äõ4P W@ VæE7vF6 6ÆVW tVæ@ ¤VæDgVæ0²W6U67&@ ¤gVæ2'6TçWB ²f÷"F2FVÖöç7G&FöâÂFRgVæ7Föâ6×Çw&FW2FRGVB6öÖÖæBöçFòFR&Æ6²67&VVà Æö6Âb33c·FW@ b33c·FWBÒuT7G&Å&VBb33c·W6W&çWF&÷ uT7G&Å6WDFFb33c¶Öæ÷WGWGFWBÂgV÷C´f÷VæBgV÷C²fײb33c·FWBfײ5$ÄbfײuT7G&Å&VBb33c¶Öæ÷WGWGFWBÂgV÷C²gV÷C² ¢6öçG&öÄfö7W2b33c¶Öæ÷WGWFæFÆRÂgV÷C²gV÷C²Âb33c·W6W&çWF&÷ ¤VæDgVæ0²'6TçW
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