Jump to content

Data validation on keypress


 Share

Recommended Posts

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 by lightwave
Link to comment
Share on other sites

  • Moderators

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
    
WEnd

All 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

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

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 by lightwave
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...