c#窗体捕捉方向键

方法1

实现方法参考代码:


        private void Form1_Load(object sender, EventArgs e)
        {

            this.KeyPreview = true;

        }
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Left || keyData == Keys.Right || keyData == Keys.Up || keyData == Keys.Down)
            {
                switch (keyData)
                {
                    case Keys.Left:
                        KeyLeftPress();
                        break;
                    case Keys.Right:
                        KeyRightPress();
                        break;
                    case Keys.Up:
                        KeyUpPress();
                        break;
                    case Keys.Down:
                        KeyDownPress();
                        break;
                }
            }
            return base.ProcessDialogKey(keyData);
        }
        public void KeyLeftPress()
        {

        }
        public void KeyRightPress()
        {

        }
        private void KeyUpPress()
        {
           
        }
        private void KeyDownPress()
        {
            
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.Text = e.KeyChar.ToString();
            
        }
        

参考链接

为什么不能触发Form1_keydown事件?-CSDN社区icon-default.png?t=N7T8https://bbs.csdn.net/topics/391894242

 C# KeyDown事件-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/fangyu723/article/details/108827544

方法2

参考代码

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Left)
        this.Left -= 100;
    if (e.KeyCode == Keys.Right)
        this.Left += 100;
    if (e.KeyCode == Keys.Up)
        this.Left -= 10;
    if (e.KeyCode == Keys.Down)
        this.Left += 10;

}

参考链接

Form窗体中判断按键信息_form1_keyup-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/wuhongyao3/article/details/3959667

特此记录

anlog

2024年1月29日

你可能感兴趣的:(笔记,c#,开发语言,方向按键,Form1_KeyPress,KeyPress)