简体   繁体   中英

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 !

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();
    }
}
switch (e.Key)
        {
            case Key.Up:
                break;
            case Key.Down:
                break;
            case Key.Left:
                break;
            case Key.Right:
                break;
            default:
                break;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM