Key functions won't work c#

148 views Asked by At

I have no problem with my code as I can see, it's a breakout game, but for testing purposes I need to be able to go up,down, left, right manually with the keys http://pastebin.com/fJWk1ifH

txtBox.KeyDown += new System.Windows.Forms.KeyEventHandler(txtBox_KeyDown); //initialisation for key presson on textbox

private void txtBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up)
    {
        xChange = 0;
        yChange = -(trackbarSpeed.Value);
    }

    if (e.KeyCode == Keys.Down)
    {
        xChange = 0;
        yChange = (trackbarSpeed.Value);
    }

    if (e.KeyCode == Keys.Left)
    {
        yChange = 0;
        xChange = -(trackbarSpeed.Value);
    }

    if (e.KeyCode == Keys.Right)
    {
        yChange = 0;
        xChange = (trackbarSpeed.Value);
    }
}
1

There are 1 answers

0
Francis Ducharme On

You could set the Form's "KeyPreview" property to true, or override the ProcessCmdKey() method as proposed here Key Events: ProcessCmdKey

The latter is the better way.