I wrote a desktop application in WPF with several buttons. It works so far but now I want to link the buttons to keys. I want the program to detect if a buttons is pressed and then perform the Button_click method. I tried to achieve this by adding this method
public void Window1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
switch (e.Key)
{
case Key.D0:
Button0_Click(new object(), new RoutedEventArgs());
break;
case Key.D1:
Button1_Click(new object(), new RoutedEventArgs());
break;
case Key.D2:
Button2_Click(new object(), new RoutedEventArgs());
break;
case Key.D3:
Button2_Click(new object(), new RoutedEventArgs());
break;
case Key.D4:
Button4_Click(new object(), new RoutedEventArgs());
break;
}
}
But it didn't work at all. How can I link the defined button methods to keys?