Page 1 of 1

Filtering user input

Posted: Mon Oct 03, 2016 11:48 pm
by trevix
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

Re: Filtering user input

Posted: Tue Oct 04, 2016 2:41 am
by dunbarx
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

Re: Filtering user input

Posted: Fri Dec 16, 2016 5:10 pm
by MaxV
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#####