- Home /
How to use TouchScreenKeyboardType with the new 4.6+ UI System?
I can't seem to find any relevant examples. (I'm using JavaScript FWIW.)
I'm assuming it's a property specific to a given InputField component, or some other UI component, but my best guess fails:
var myField : UI.InputField;
...
myField.TouchScreenKeyboard.TouchScreenKeyboardType = TouchScreenKeyboardType.ASCIICapable;
I can control myField successfully in many ways, but this line just generates BCE0019: 'TouchScreenKeyboard' is not a member of 'UnityEngine.UI.InputField'.
(Ultimate goal: prevent Emoji entry. I'm assuming that's what "ASCIICapable" does.)
TIA
Answer by Bunny83 · May 13, 2015 at 01:22 AM
TouchScreenKeyboard is a class. So the way you try to use it makes no sense at all ^^.
InputField has a property called keyboardType which allows you to specify which keyboard type it should use. So you can do:
myField.keyboardType = TouchScreenKeyboardType.ASCIICapable;
Thanks! I was trying to guess the property name, but when it didn't match the class name, and searching the InputField docs for "TouchScreen$$anonymous$$eyboard" came up blank, I was stumped!
Happy to say, I'm now Emoji-free :) (And as I'd hoped, accents are still allowed even if not technically ASCII.)
If you have problems inputting non ASCII characters you might use the default keyboard but filter the input and only allow characters you want. The InputField has a delegate called onValidateInput. It takes the current text, the position in the text and the new character as parameter and you should return the character that will be added.
Even though it's not mentioned in the docs i'm sure when you return character 0 ('\0') it won't add anything and you filtered the input out.
You can use a dictionary or array of allowed / forbidden characters to check if a certain character is allowed.
public char OnValidateInput(string text, int charIndex, char addedChar)
{
if (!allowedChars.Contains(addedChar))
return '\0';
if (forbiddenChars.Contains(addedChar))
return '\0';
return addedChar;
}
myField.onValidateInput += OnValidateInput;
If you use a dictionary you might want to use Contains$$anonymous$$ey ins$$anonymous$$d. Of course you can do whatever you want inside that callback. Just keep in $$anonymous$$d that it's called for each character you type.
Thanks! I just wanted a keyboard that can type exactly what Unity is capable of rendering--which includes accents, so I've avoided the need for validation.
(I have not begun to explore supporting non-Latin character sets/languages, and I can't imagine it's easy to support, say, Chinese, but that's a project for another day!)