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
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
My questions are: is this the best way to do it? will it works on windows keyboards as well? What about other languages?
Trevix