【问题标题】:Program doesn't capture the key程序未捕获密钥
【发布时间】:2023-04-04 18:14:01
【问题描述】:

正如标题所说,我的程序有问题。我制作了一个函数(一个战斗函数,玩家在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
        }
    }
}

如果需要,我会将其余代码放在下一次编辑中。

【问题讨论】:

  • 你的label(猜猜joc)是哪种控制方式?有重点吗?如果另一个控件将获得焦点,则它不会获得关键事件。
  • 你确定这些布尔语句能满足你的要求吗? (true || false && false) == true, 但是 (false || true && false) == false。
  • @AdrianoRepetti 是textbox,对不起
  • @PrestonGuillot 是的,我敢肯定,我的意思是如果他们没有做我想做的事,为什么他们在我 alt-tab 之后工作?
  • 因为您还没有测试过permite_pasi 为假的案例。

标签: c# key


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-04
    • 2011-05-22
    • 2022-07-23
    • 2012-09-11
    • 2021-02-24
    • 2017-12-04
    • 2014-02-15
    相关资源
    最近更新 更多