如何检测何时按下箭头键/ C# WPF

bil*_*_56 2 c# wpf events datagrid

我正在使用 WPF C# 应用程序,当按下键盘上的箭头键时,我需要执行一些操作,例如:

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();
}
Run Code Online (Sandbox Code Playgroud)

我根本无法在 wpf 中弄清楚如何检测向下箭头键 .. 任何形式的帮助都会很棒!

谢谢 !

Tho*_*kow 5

属性中KeyEventArgs有关按下键的保持信息KeyEventArgs.Key,因此您可以通过检查是否e.Key等于 来检查向下箭头键Key.Down,这是向下箭头键的枚举值。

private void Window_OnKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Down) // The Arrow-Down key
    {
        DoSomething();
    }
}
Run Code Online (Sandbox Code Playgroud)