程序无法捕获密钥

时间:2015-05-14 15:51:12

标签: c# key

正如标题所说,我的程序有问题。我做了一个功能(一个战斗功能,玩家在textbox中写下并按下一个按钮)。战斗完成后,我的程序不会捕获按下的键,除非我在程序中再次使用alt-tab和alt-tab。我该怎么做?

//capture key function     
private void joc_KeyDown_1(object sender, KeyEventArgs e)
{

    if (e.KeyCode == Keys.F4 && e.Alt && permite_pasi == true)
    {
        enableClose = false;
    }

    if (e.KeyCode == Keys.D || e.KeyCode == Keys.Right && permite_pasi == true)
    {
        step(6); //step() is the movement function
    }

    if (e.KeyCode == Keys.W || e.KeyCode == Keys.Up && permite_pasi == true)
    {
        step(8);
    }

    if (e.KeyCode == Keys.A || e.KeyCode == Keys.Left && permite_pasi == true)
    {
        step(4);
    }

    if (e.KeyCode == Keys.S || e.KeyCode == Keys.Down && permite_pasi == true)
    {
        step(2);
    }
}

//movement function
public void step(int directie)
{
    if (directie == 6 && checkstep(directie) == true) { //checkstep() is a function which check the environment for obstacles
        if (check_NPC(jucator.x, jucator.y) > 0) // function for checking the environment
        {
            scorenpc = check_NPC(jucator.x, jucator.y); //some variabile
            NPC(check_NPC(jucator.x, jucator.y), jucator.score); // the battle function
        }
        else
        {
            //movement instructions
        }
    }
    if (directie == 4 && checkstep(directie) == true)
    {
        if (check_NPC(jucator.x, jucator.y) != 0)
        {
            scorenpc = check_NPC(jucator.x, jucator.y);
            NPC(check_NPC(jucator.x, jucator.y), jucator.score);
        }
        else
        {
            //movement instructions
        }
    }
    if (directie == 8 && checkstep(directie) == true)
    {
        if (check_NPC(jucator.x, jucator.y) > 0)
        {
            scorenpc = check_NPC(jucator.x, jucator.y);
            NPC(check_NPC(jucator.x, jucator.y), jucator.score);
        }
        else
        {
            //movement instructions
        }
    }
    if (directie == 2 && checkstep(directie) == true)
    {
        if (check_NPC(jucator.x, jucator.y) != 0)
        {
            scorenpc = check_NPC(jucator.x, jucator.y);
            NPC(check_NPC(jucator.x, jucator.y), jucator.score);
        }
        else
        {
            //movement instructions
        }
    }
}

如果必要的话,我会将其余的代码放在下一个编辑中。

1 个答案:

答案 0 :(得分:0)

在战斗功能结束时,我使用this.Focus()