c#按键键盘事件监控上下左右和其它按键

c#按键键盘事件监控键盘上下左右和其它按键

重写:protected override bool ProcessDialogKey(Keys keyData)就可以实现了。

放在窗口类里面

 public partial class Form_XYZ_Jog : Form

{

xxxxx

}

        protected override bool ProcessDialogKey(Keys keyData)
        {
            // MessageBox.Show(keyData.ToString());
            string direction2 = keyData.ToString();
            string Left = "A";
            string Right = "D";
            string Front = "W";
            string Back = "S";
            string Up = "Up";
            string Down = "Down";
            label_press_msg.Text = "已按下:" + direction2;
            Console.WriteLine(DateTime.Now.ToString()+ label_press_msg.Text);
          
            if (direction2 ==Left)
            {
                button_move_left.Focus();

                EnableLeftRightFrontBack(true);
                EnableUpDown(false);
            }
            else if(direction2 == Right)
            {
                button_move_right.Focus();
                EnableLeftRightFrontBack(true);
                EnableUpDown(false);
            }
            else if (direction2 == Front)
            {
                button_move_front.Focus();
                EnableLeftRightFrontBack(true);
                EnableUpDown(false);
            }
            else if (direction2 == Back)
            {
                button_move_back.Focus();
                EnableLeftRightFrontBack(true);
                EnableUpDown(false);
            }
            else if (direction2 == Up)
            {
                button_move_down.Focus();
                EnableLeftRightFrontBack(false);
                EnableUpDown(true);
            }
            else if (direction2 == Down)
            {
                button_move_up.Focus();
                EnableLeftRightFrontBack(false);
                EnableUpDown(true);
            }
            return base.ProcessDialogKey(keyData);
        }

猜你喜欢

转载自blog.csdn.net/txwtech/article/details/131445158