What is a better way to respond to numeric keypresses?

43 views Asked by At

I want to allow the checking of radio buttons via the keyboard, specifically via the numbers 1, 2, 3. I've got this code that works:

// "a WPF form hears what it wants to hear and disregards the rest" - paraphrased from "The Boxer" by Simon & Garfunkel
private void Window_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs keArgs)
{
    var keyPressed = keArgs.Key.ToString();
    if ((keyPressed.Equals("D1")) || (keyPressed.Equals("NumPad1")))
    {
        rdbtnCandidateVal1.IsChecked = true;
    }
    else if ((keyPressed.Equals("D2")) || (keyPressed.Equals("NumPad2")))
    {
        rdbtnCandidateVal2.IsChecked = true;
    }
    else if ((keyPressed.Equals("D3")) || (keyPressed.Equals("NumPad3")))
    {
        rdbtnCandidateVal3.IsChecked = true;
    }
}

...but know this is kludgy'verbose. What is a better way to take action for a subset of keys and disregard the rest?

0

There are 0 answers