Filtering user input

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
trevix
Posts: 1079
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Filtering user input

Post by trevix » Mon Oct 03, 2016 11:48 pm

On OSX LC 8.1.1rc1 I am trying to do a correct filtering of user input, according to the kind of field that is presented (Date, numbers, text).
Living in Italy, where we have tons of accented words, I discovered that, for example, the "è" and the "[" (same key with the optionKey down) report the same raw number.
On top of that I need to filter chars like []|"& etc, since the content of the fields get used in other script (with valuations and itemdelimiters).
Also I want the user to be able to use the number keyboard (on Mac and Windows).

So, given 3 kind of fields, each with the following script (replacing pKind with "date","number" and text:

Code: Select all

on RawKeyDown pKey
    if KeyFiltering(pKey,pKind) is empty then beep
    else pass RawKeyDown
end RawKeyDown
I came up with the following card script (see attachment stack):

Code: Select all

--------------------------------Keyboard filtering from various input fields
function KeyFiltering pKeyCode pMode --pKey is the key
    put numToCodepoint(pKeyCode) into pKey
    --allow
    if  pKeyCode is among the words of "65288 65293 65361 65363" then return pKeyCode --backspace return arrowleft arrowright
    --stop
   if  the optionkey is down and pKeyCode is among the words of "91 93 47" then return empty --[,],°
    --it depends
    switch pMode
        case "Date" --05/12/2016
            if pKey is among the characters of "0123456789/" or  pKeyCode is among the words of "65438 65436 65433 65435 65430 65437 65432 65429 65431 65434  65455" then return pKeyCode
            break
        case "Text"
            --avoid special characters too
            if pKey is not among the characters of "*^|#" & quote  then return pKeyCode --",[,]
            break
        case "number"
            --space 0123456789+-*/()
            if pKey is among the characters of "0123456789+-*/,^()"  or  pKeyCode is among the words of "32 65438 65436 65433 65435 65430 65437 65432 65429 65431 65434 65451 65453 65450 65455" then return pKeyCode
            break
        default --pMode is empty= is a label
            return pKeyCode --anything allowed
    end switch
end KeyFiltering
All this works.
My questions are: is this the best way to do it? will it works on windows keyboards as well? What about other languages?

Trevix
Attachments
InputFiltering.livecode.zip
try some text input
(1.61 KiB) Downloaded 256 times
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10322
Joined: Wed May 06, 2009 2:28 pm

Re: Filtering user input

Post by dunbarx » Tue Oct 04, 2016 2:41 am

Hmmm.

Yes, modifier keys (except for Shift) report the same rawKeyDown value as if they were not included at all. Perhaps there is a unicode solution for this? Someone will chime in.

Craig Newman

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Filtering user input

Post by MaxV » Fri Dec 16, 2016 5:10 pm

What do you think of this? One keydown for each field:
########CODE#######
#numbers
on keydown pkey
if pkey is a number then
pass keydown
end if
end keydown

#only text, no numbers
on keydown pkey
put chartonum(pkey) into nkey
if (nkey >= 65 AND nkey <= 163 ) OR nkey = 32 then
pass keydown
end if
end keydown

#date
on keydown pkey
if pkey is a number or pkey = "/" then
put the number of chars of me into nnn
if nnn = 2 or nnn=5 then put "/" after me
pass keydown
end if
end keydown
#####END OF CODE#####
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply