Can I determine if a KeyEventArg is an letter or n

2020-08-09 09:42发布

问题:

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))

回答1:

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

bool isLetterOrDigit = char.IsLetterOrDigit((char) keyEventArgs.KeyCode);


回答2:

Char.IsNumber() and Char.IsLetter()



回答3:

In WPF? Use PreviewTextInput or TextInput events instead of KeyDown