Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when arrow key down is pressed / C# WPF

I'm working with WPF C# app, and I need to implement some action when arrow key down on keyboard is pressed, for example:

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    // Here I gotta check is that arrow key down which invoked this event.. then call a method
    DoSomething();
}

I simply can not figure out in wpf how to detect a arrow key down .. Any kind of help would be great!

Thanks !

like image 817
billy_56 Avatar asked Nov 08 '25 12:11

billy_56


1 Answers

The KeyEventArgs hold information about the pressed key in the KeyEventArgs.Key property and so you can check for the down arrow key by checking if e.Key is equal to Key.Down, which is the enumeration value for the arrow down key.

private void Window_OnKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Down) // The Arrow-Down key
    {
        DoSomething();
    }
}
like image 112
Thomas Flinkow Avatar answered Nov 10 '25 03:11

Thomas Flinkow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!