tips
[Top] [All Lists]

Filtering specific keys in an editfield

To: "REALbasic Tips" <REALbasic-tips at lists dot realsoftware dot com>
Subject: Filtering specific keys in an editfield
From: Geoff Perlman <geoff at realsoftware dot com>
Date: Thu, 03 Feb 2000 11:52:29 -0600
Sometimes you will want to allow only specific characters to be entered into an editfield by the user. It's actually quite easy to add it yourself. You can do it by comparing the key pressed in the keydown event of the editfield
to a list of allowable characters defined in a constant.

Just enter the following code into the "KeyDown" event:

Function KeyDown(Key As String) As Boolean

CONST kAllowedChars = "1234567890,."  //allow the user to enter these

if asc(Key)<32 then
 return false
end if

if instr(kAllowedChars,key) <> 0 then
return false
end if

return true
End Function

Here's an explanation of what the code does:

1. First, it checks if the key pressed was a control key, such as a cursor
key, backspace or return. Their ascii codes are all less than 32 so it's
easiest to allow all keys with an ascii value below 32.

2. Next, it uses the Instr function check if the key pressed is in the
pre-defined constant "kAllowedChars". If it is, false is returned so the key
is allowed. Returning true would filter the keystroke.

3. If this isn't the case, we can assume that the key should not be allow
and return true to tell the EditField to filter it.

Thanks to Sascha René Leib for contributing this tip.
--

Geoff Perlman
REAL Software, Inc.
http://www.realsoftware.com
mailto:geoff at realsoftware dot com



 - - - - - - - - - -
For list commands, send "Help" in the body of a message to
<requests at lists dot realsoftware dot com>



<Prev in Thread] Current Thread [Next in Thread>
  • Filtering specific keys in an editfield, Geoff Perlman <=