KeyDown事件无效 [英] KeyDown Event is not working

查看:258
本文介绍了KeyDown事件无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

请参阅下面的代码(我正在尝试制作一个蛇游戏)。但是它发现keydown事件无效。

Hi All,
Please see my below code(I am trying to make a snake game).But its found that the keydown event is not working.

public partial class Form1 : Form
    {
        Button Head = null;
        string Direction = "R";
        public Form1()
        {
            InitializeComponent();
            this.KeyPreview = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Button btn = new Button();
            btn.BackColor = Color.Red;
            btn.Text = "";
            btn.Size = new Size(12, 12);
            btn.Location = new Point(10, 10);
            Head = btn;
            this.Controls.Add(Head);
            MoveTimer.Start();
        }

        private void MoveTimer_Tick(object sender, EventArgs e)
        {
            MoveSnake();
        }

        private void MoveSnake()
        {
            switch (Direction)
            {
                case "R" :
                    Head.Location = new Point(Head.Location.X + 1, Head.Location.Y);
                    break;
                case "L":
                    Head.Location = new Point(Head.Location.X - 1, Head.Location.Y);
                    break;
                case "U":
                    Head.Location = new Point(Head.Location.X , Head.Location.Y -1);
                    break;
                case "D":
                    Head.Location = new Point(Head.Location.X, Head.Location.Y + 1);
                    break;
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {

                case Keys.Down:
                    Direction = "D";
                    break;

                case Keys.Left:
                    Direction = "L";
                    break;

                case Keys.Right:
                    Direction = "R";
                    break;


                case Keys.Up:
                    Direction = "U";
                    break;

            }
        }
    }



请有人帮我这个....


Please Anyone can help me on this....

推荐答案

您好,



要处理箭头键,ESC,TAB,RETURN,您必须覆盖IsInputKey方法。您可以在此处找到更多信息:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown%28v=vs.110%29.aspx [ ^ ]



您可以覆盖Form的ProcessCmdKey方法,而不是使用KeyDown事件。这应该可以解决问题:



Hi,

To handle arrow keys, ESC, TAB, RETURN you must override IsInputKey method. More informations you can find here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown%28v=vs.110%29.aspx[^]

Instead of using KeyDown event you can override Form's ProcessCmdKey method. This should do the trick:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Down)
    {
        Direction = "D";
        return true;
    }
    else if (keyData == Keys.Left)
    {
        Direction = "L";
        return true;
    }
    else if (keyData == Keys.Right)
    {
        Direction = "R";
        return true;
    }
    else if (keyData == Keys.Up)
    {
        Direction = "U";
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);

}





干杯!



Cheers!


你可能有一个焦点问题...

你在for上的按钮保持焦点,所以任何关键事件都会进入它,你的窗口什么都没有...

试试这个:

创建一个面板作为游戏区域(而不是整个窗口客户区),在开始时将焦点设置在该面板上......现在关键事件将转到该面板,因此添加您的事件面板的处理程序......
You probably have a focus problem...
The button you have on the for holds the focus, so any key event goes to it and your window gets nothing...
Try this:
Create a panel as you area of the game (and not the whole window client area), set the focus on that panel upon start...Now key events will go to that panel, so add your event handler to the panel...


这篇关于KeyDown事件无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆