C# 捕获光标键的KeyDown和KeyUp事件
我想在winfor应用程序中移动一些图形。为此,我需要知道是否有任何光标键被按下。我试图重写ProcessCmdKey,但没有成功 如何实现这一点,有什么建议/想法吗C# 捕获光标键的KeyDown和KeyUp事件,c#,winforms,key,keydown,keyup,C#,Winforms,Key,Keydown,Keyup,我想在winfor应用程序中移动一些图形。为此,我需要知道是否有任何光标键被按下。我试图重写ProcessCmdKey,但没有成功 如何实现这一点,有什么建议/想法吗 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { switch (keyData) { case Keys.Left: // while Left is down
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Left:
// while Left is down
// call this method repeadetdly
buttonGoLeft();
// when key is up stop calling this method
// and check for other keys
return true;
//Other cases
}
}
这管用
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
KeyPreview = true;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Left:
// while Left is down
// call this method repeadetdly
MessageBox.Show("its left", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
// when key is up stop calling this method
// and check for other keys
return true;
default:
return false;
}
}
}
}
表单没有捕获事件吗?它捕获了事件,但我无法设置while循环,因此当按下任何光标键时,我需要的方法会被重复调用。
ProcessCmdKey
方法不会被重复调用吗?是否有form.KeyPreview=true?默认情况下,该值为false,将其设置为true!有什么理由反对投票吗?