Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine is key pressed is alphanumeric in Windows Runtime

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?

like image 320
JQuery Mobile Avatar asked Nov 27 '12 13:11

JQuery Mobile


1 Answers

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
    }
}
like image 54
emartel Avatar answered Oct 14 '22 00:10

emartel