【问题标题】:How to detect when arrow key down is pressed / C# WPF如何检测何时按下箭头键/ C# WPF
【发布时间】:2018-04-12 07:18:28
【问题描述】:

我正在使用 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();
}

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

谢谢!

【问题讨论】:

    标签: c# wpf events datagrid


    【解决方案1】:

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

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

    【讨论】:

    • 单人还是双人=?
    • == 抱歉,我将编辑答案。讨厌的错字,它甚至无法编译;)
    【解决方案2】:
    switch (e.Key)
            {
                case Key.Up:
                    break;
                case Key.Down:
                    break;
                case Key.Left:
                    break;
                case Key.Right:
                    break;
                default:
                    break;
            }
    

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2016-11-11
      • 1970-01-01
      • 2014-09-03
      • 2021-12-15
      相关资源
      最近更新 更多