如何捕获方向键

时间:2021-10-16 21:56:37

在窗体中添加如下的重写方法来处理按键的信息:

/*键盘事件*/
        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Tab: MessageBox.Show(keyData.ToString());
                    break;
                case Keys.Left: MessageBox.Show(keyData.ToString());
                    break;
                case Keys.Right: MessageBox.Show(keyData.ToString());
                    break;
            }
            if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right)
                return false;
            else
                return base.ProcessDialogKey(keyData);
        }