[英]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: 我正在使用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();
}
I simply can not figure out in wpf how to detect a arrow key down .. Any kind of help would be great! 我根本无法在wpf中弄清楚如何检测到向下的箭头键。任何帮助都将非常有用!
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. 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();
}
}
switch (e.Key)
{
case Key.Up:
break;
case Key.Down:
break;
case Key.Left:
break;
case Key.Right:
break;
default:
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.