0

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?

  • Can you give us a bit more information. Do the execution never gets to the _keyDown event or is the "e.key" giving the wrong value. – nizoodxs Jan 10 '20 at 06:29
  • Does this answer your question? [How can I capture KeyDown event on a WPF Page or UserControl object?](https://stackoverflow.com/questions/347724/how-can-i-capture-keydown-event-on-a-wpf-page-or-usercontrol-object) – Gonzo345 Jan 10 '20 at 06:30
  • use `buttonToCalls.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));` – Saif Jan 10 '20 at 06:34
  • if your method can detect the key event and cannot raise button click event, then use the command mentioned in my previous comment – Saif Jan 10 '20 at 06:40

2 Answers2

0

You need to implement key event handler so for example for key Enter

Refer to Microsoft docs here

XAML code

<StackPanel>
 <TextBlock Width="300" Height="20">
    Type some text into the TextBox and press the Enter key.
  </TextBlock>
  <TextBox Width="300" Height="30" Name="textBox1"
           KeyDown="OnKeyDownHandler"/>
  <TextBlock Width="300" Height="100" Name="textBlock1"/>
</StackPanel>

key event handler in backend

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
       //do your things here 
    }
}
Saif
  • 2,611
  • 3
  • 17
  • 37
0

create an event when the main window is loaded :

in XAML :

Loaded="MainWindow_Loaded"

in code behind :

  private void MainWindow_Loaded(object sender, RoutedEventArgs e) 
  {
    var window = Window.GetWindow(this);
    window.KeyDown += HandleKeyPress;
  }

Handle your key pressed event :

private void HandleKeyPress(object sender, 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;
            ....
}
Erwin Draconis
  • 764
  • 8
  • 20