关键功能不起作用c#

时间:2013-12-12 21:03:43

标签: c# breakout

我的代码没有任何问题,因为我可以看到,这是一个突破性的游戏,但出于测试目的,我需要能够使用键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 个答案:

答案 0 :(得分:1)

您可以将Form的“KeyPreview”属性设置为true,或覆盖此处建议的ProcessCmdKey()方法Key Events: ProcessCmdKey

后者是更好的方式。