- Home /
only detect numerical keyboard input
hello i would like to only detect key input if it a number is pressed, I am using the following code.
Regex rgx = new Regex("[^0-9]");
if (e.isKey)
Debug.Log("Detected character: " + e.character);
I cannot combine these two bits to get it to only detect the number input from the keyboard.
Any help is appreciated
p.s. i am using c#
Answer by Eric5h5 · Mar 11, 2013 at 02:29 PM
Check Event.keycode and whether it's Keypad0, Keypad1, etc.
Answer by Statement · Mar 11, 2013 at 02:31 PM
Why would you use Regular Expressions to detect if a numeric key was pressed? Sounds like you got the wrong tool for the job. You could use char.IsDigit to find out if it is a digit.
void OnGUI () {
Event e = Event.current;
bool isKeyDown = e.type == EventType.KeyDown;
bool isDigit = char.IsDigit(e.character);
if (isKeyDown && isDigit)
Debug.Log("Digit character pressed: " + e.character);
}
I try Your code and the script do well... but then the GUI interface ( I used GUI text area ) didn't appear. So, where I must put the GUI.TextArea line? thx alot
Your answer
Follow this Question
Related Questions
how to get in game numerical input 1 Answer
How to draw an arrow from fps contoller 1 Answer
Mathf.pingpong 4 Answers
turn on off particle system 1 Answer