Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 捕获光标键的KeyDown和KeyUp事件_C#_Winforms_Key_Keydown_Keyup - Fatal编程技术网

C# 捕获光标键的KeyDown和KeyUp事件

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

我想在winfor应用程序中移动一些图形。为此,我需要知道是否有任何光标键被按下。我试图重写ProcessCmdKey,但没有成功

如何实现这一点,有什么建议/想法吗

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!有什么理由反对投票吗?