Click here to Skip to main content
15,905,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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....
Posted
Comments
Have you debugged? Is it going inside the event?
Manu Prasad 15-Dec-14 1:53am    
No, its not going inside the event.
BillWoodruff 15-Dec-14 2:01am    
You put a break-point on the line switch (e.KeyCode) and you run your app and press one of the arrow-keys ... and it never reaches that break-point ?
Are you sure, you have assigned the event to the form. Check form properties.
Manu Prasad 15-Dec-14 1:56am    
Hi all I just disabled the button then its working. Can Anyone tell why?

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:

C#
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!
 
Share this answer
 
Comments
BillWoodruff 15-Dec-14 3:08am    
+5 I recall that this worked for me ... some years ago ... without having to implement 'IsInputKey. Can you confirm that ? thanks, Bill

Also, note that by using this technique it is not necessary to implement any other Key related Event of the Form, or set the Form 'KeyPreview property to 'true ... unless, of course, you need to trap other keys for some reason.
Marcin Kozub 15-Dec-14 3:11am    
Thx Bill!
Yes, it is working fine. I'm using this solution all the time :)
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...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900