I'm working on an app for Windows 8
.
I'm trying to determine if a key pressed is alphanumeric. The KeyRoutedEventArgs
class doesn't seem to provide any helpers.
Is there something I'm overlooking? What is the best way to determine if the user entered a letter or number?
The KeyRoutedEventArgs
contains a Key
value which is a VirtualKey.
You can simply test if the Key
is within the codes you want to support from this list.
For example:
private void MyKeyDown(object sender, KeyRoutedEventArgs e)
{
int keyValue = (int)e.Key;
if ((keyValue >= 0x30 && keyValue <= 0x39) // numbers
|| (keyValue >= 0x41 && keyValue <= 0x5A) // letters
|| (keyValue >= 0x60 && keyValue <= 0x69)) // numpad
{
// do something
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With