lightwave Posted April 13, 2009 Share Posted April 13, 2009 (edited) Hi guys,I'm creating my first GUI. I've gone with the Event mode. Firstly, I've been trying to find an event for when a key is pressed on the keyboard. There appear to be primary and secondary mouse button events, but I can't see anything to trigger an event (and run a function) each time a key is pressed.I'd like to create a function that's called each time a key is pressed in an Inputbox control which will check that the character is valid. If it isn't, I'd like to ignore the keypress and display a balloon tooltip which reads something like "This field can only contain the following characters: 0-9 ." or whatever. Basically replicating the functionality of Windows (XP & Vista) Explorer file renaming. Try editing a filename and typing "/". That's what I'm trying to do.I'd show some example code of my progress, but I don't have any yet. I'm stuck trawling the help file for a way to determine keypresses so I can trigger an event and write a function. I imagine this would work nicely using regular expressions, which I'm not yet familiar with.Can anybody get me started? Is there an event triggered by a keypress in AutoIt? Something like onkeydown?Any help/suggestions always appreciated! Thank you,Al Edited April 13, 2009 by lightwave Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 13, 2009 Moderators Share Posted April 13, 2009 lightwave,I apologise in advance because this requires StringRegExp which is far from the easiest function to understand, but this is the best way I have come across to do what you want. Oh, and it uses an Input control, not an Inputbox - sorry:#include <GUIConstantsEx.au3> GUICreate("Test", 500, 500) $hEdit = GUICtrlCreateInput("", 10, 10, 300, 20) $aPos = WinGetPos("Test", "") GUISetState() $sPreviousInput = "" While 1 If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit $sInput = GuiCtrlRead($hEdit) If $sPreviousInput <> $sInput Then If StringRegExp($sInput, '[^[:digit:]]') Then $sInput = StringTrimRight($sInput, 1) GUICtrlSetData($hEdit, $sInput) ToolTip("This field can only contain the following characters: 0-9", $aPos[0] + 10, $aPos[1] + 50) Sleep(2000) ToolTip("") GUICtrlSetState($hEdit, $GUI_FOCUS) Send("{END}") EndIf EndIf $sPreviousInput = $sInput WEndAll you have to do is to change the StringRegEx parameters to match your requirements. There are lots of gurus here to help you if it is a complex match - I withdraw at this point.....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 Link to comment Share on other sites More sharing options...
lightwave Posted April 13, 2009 Author Share Posted April 13, 2009 Ok, cool - I see what you've done. That's the ticket, thank you! If I improve upon it, I'll post it up. Al Link to comment Share on other sites More sharing options...
lightwave Posted April 14, 2009 Author Share Posted April 14, 2009 (edited) Ok, here we go. I've got some better results this way, as previously it allowed mashing the keyboard to input extraneous characters. Now you really have to go to town on the keyboard to bust the code: #include <GUIConstantsEx.au3> #include <Timers.au3> Global $toolTipTimeout GUICreate("Test", 500, 500) $hEdit = GUICtrlCreateInput("", 10, 10, 300, 20) $aPos = WinGetPos("Test", "") GUISetState() $sPreviousInput = "" While 1 If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit $sInput = GuiCtrlRead($hEdit) If StringRegExp($sInput, '[^[:digit:]]') Then $sInput = StringTrimRight($sInput, 1) GUICtrlSetData($hEdit, $sInput) ToolTip("This field can only contain the following characters: 0-9", $aPos[0] + 10, $aPos[1] + 50, "", 0, 1) $toolTipTimeout = TimerInit() GUICtrlSetState($hEdit, $GUI_FOCUS) Send("{END}") EndIf If TimerDiff($toolTipTimeout) > 2000 Then ToolTip("") $sPreviousInput = $sInput WEnd On a separate note, for some reason this script wouldn't run unless I declared my $toolTipTimeout variable. It threw the error "Variable used without being declared", even though nothing else has been declared and the option for MustDeclareVars isn't enabled. Can someone else try this script with/without the declaration and tell me if it's just me, or if it doesn't work - why not? Thank you! Al Edited April 14, 2009 by lightwave Link to comment Share on other sites More sharing options...
Authenticity Posted April 14, 2009 Share Posted April 14, 2009 Because there is no static-like keyword in AutoIt so you're checking TimerDiff on a not defined variable. Link to comment Share on other sites More sharing options...
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