0

我想在 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
     }
  }
4

1 回答 1

1

这行得通!

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;
            }
        }
    }
}
于 2012-12-17T14:34:12.523 回答