- Home /
Redirecting input with event.current.character?
Soo I searched around on the forum, like a beginner should, and I found what I was looking for [here][1], but it was in C#, and I am working in JavaScript. So I tried to understand how the code worked, and rewore as much as possible. I changed it into this, onGui:
var InputKey = Event.current.character;
if (InputKey == 'a')
{
//Remove the inputtet character
}
Now, this was just an example to get an idea of how it worked, for a single letter, but couldn't get that to work either. Anyone mind retouching the code so it works as intented, eg. restricts any non character a-z or number 0-9 from being entered (without using RegEx, due to Webplayer target)
Thank you in advance, and may the new year return less compile error than the previous! [1]: http://answers.unity3d.com/questions/18736/restrict-characters-in-guitextfield.html
Answer by Eric5h5 · Jan 01, 2013 at 12:29 AM
Unityscript doesn't use the single-quote method for indicating chars (both single and double quotes are used for strings). Instead refer to the index of the string. If you're using a single character in a string, naturally the only index possible is 0.
if (Event.current.character == "a"[0]) {
Event.current.character = "\0"[0];
}
Note that \\0 is an escaped sequence meaning "null char", so it's actually one character even though it looks like two.
This doesn't work. I appreciate your correction of the syntax, but it doesn't solve my problem, of restricting user input in text field, to only letters and numbers.
It does work. Look at my answer in the question you linked to, then substitute the syntax I showed above in order to use it in Unityscript.