Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I determine if a KeyEventArg is an letter or number?

Is there a way to determine if a key is letter/number (A-Z,0-9) in the KeyEventArgs? Or do I have to make it myself? I found a way with e.KeyCode, is that accurate?

if(((e.KeyCode >= Keys.A       && e.KeyCode <= Keys.Z )
 || (e.KeyCode >= Keys.D0      && e.KeyCode <= Keys.D9 )
 || (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9))
like image 589
Andr Avatar asked Mar 13 '11 23:03

Andr


3 Answers

You can use the char.IsLetterOrDigit() method on the KeyCode of the event args:

bool isLetterOrDigit = char.IsLetterOrDigit((char) keyEventArgs.KeyCode);
like image 176
adrianbanks Avatar answered Nov 03 '22 23:11

adrianbanks


Char.IsNumber() and Char.IsLetter()

like image 9
Bala R Avatar answered Nov 03 '22 21:11

Bala R


In WPF? Use PreviewTextInput or TextInput events instead of KeyDown

like image 8
Tank Gamer Avatar answered Nov 03 '22 23:11

Tank Gamer