KeyState 条件不起作用

KeyState condition is not working

我正在制作一个简单的飞机射击游戏,想自由移动飞机并同时开火,但我可以左移+开火、右+开火、左上+开火、左下+ 开火但右下 + 开火和右上 + 开火条件不工作我已经尽力了。以下是我的代码(这里的布尔检查是为了火):

KeyDown:

private void Form1_KeyDown(object sender, KeyEventArgs e)
        {


            GetKeyboardState(keys);

            if ((keys[(int)Keys.Down] & 128) == 128)
            {
                moveDown = true;
                if ((keys[(int)Keys.Down] & keys[(int)Keys.Right] & 128) == 128)
                {

                    moveRight = true;
                }
                else if ((keys[(int)Keys.Down] & keys[(int)Keys.Left] & 128) == 128)
                {
                    moveLeft = true;
                }
                if ((keys[(int)Keys.Space] & 128) == 128)
                {
                    check = true;
                    Console.WriteLine("fire");
                }

            }
            else if ((keys[(int)Keys.Up] & 128) == 128)
            {

                    moveUp = true;
                    Console.WriteLine("up " + plane.Location.Y);

                    if ((keys[(int)Keys.Up] & keys[(int)Keys.Right] & 128) == 128)
                    {
                        moveRight = true;
                        Console.WriteLine("(" + plane.Location.X + "," + plane.Location.Y + ")");  
                    }

                    else if ((keys[(int)Keys.Up] & keys[(int)Keys.Left] & 128) == 128 )
                    {

                        moveLeft = true;
                        Console.WriteLine("(" + plane.Location.X + "," + plane.Location.Y + ")");
                    }

                    if ((keys[(int)Keys.Space] & 128) == 128)
                    {
                        check = true;
                        Console.WriteLine("fire");
                    }

             }

            else if ((keys[(int)Keys.Right] & 128) == 128)
            {

                        moveRight = true;
                if ((keys[(int)Keys.Space] & 128) == 128)
                {
                    check = true;
                }
            }

            else if ((keys[(int)Keys.Left] & 128) == 128)
            {
                    moveLeft = true;

                if ((keys[(int)Keys.Space] & 128) == 128)
                {
                    check = true;
                } 
            }
            else if ((keys[(int)Keys.Space] & 128) == 128)
            {
                    check = true;  
            }


        }

定时器:

private void shootTimer_Tick(object sender, EventArgs e)
        {
            if (check == true)
            {
                checkShots();
            }

            moveShot();

            if (moveRight == true)
            {
                if (plane.Location.X < 545)
                {
                    plane.Location = new Point(plane.Location.X + 4, plane.Location.Y);
                    restartShot.Location = new Point(restartShot.Location.X + 4, restartShot.Location.Y);
                }
            }
            if (moveLeft == true)
            {
                if (plane.Location.X > 0)
                {
                    plane.Location = new Point(plane.Location.X - 4, plane.Location.Y);
                    restartShot.Location = new Point(restartShot.Location.X + 4, restartShot.Location.Y);
                }
            }
            if (moveUp == true)
            {
                if (plane.Location.Y > 0)
                {
                    plane.Location = new Point(plane.Location.X, plane.Location.Y - 4);
                    restartShot.Location = new Point(restartShot.Location.X, restartShot.Location.Y - 4);
                }
            }
            if (moveDown == true)
            {
                if (plane.Location.Y < 342)
                {
                    plane.Location = new Point(plane.Location.X, plane.Location.Y + 4);
                    restartShot.Location = new Point(restartShot.Location.X, restartShot.Location.Y + 4);
                }
            }

        }

这受限于硬件。键盘通常使用键的硬件矩阵,并由一个非常简单的处理器驱动,该处理器不允许同时进行任意键组合。要知道,键盘最初是用来写文字的,一次最多只能按一个键+shift/alt/ctrl。解决您问题的唯一安全方法是使用合适的游戏设备,例如键盘。

您也可以尝试购买不同的键盘 - 它们并不相同。与其他人相比,他们中的一些人一次允许更多的钥匙。而且它始终取决于您同时按下了哪些特定的键。有些组合有效,有些则无效。

您可以同时检测multiple-keypress

public partial class Form1 : Form
{
    Timer timer1;

    public Form1()
    {
        InitializeComponent();
        this.KeyDown += Form1_KeyDown;
        KeyUp += Form1_KeyUp;
        Load += Form1_Load;
        timer1 = new Timer();
        timer1.Tick += timer1_Tick;
    }


    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (keyleft && keyup)
        {
            Console.Beep(220, 300);
        }
    }
    bool keyup = false;
    bool keyleft = false;

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Up)
        {
            keyup = true;
        }
        else if (e.KeyCode == Keys.Left)
        {
            keyleft = true;
        }
    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Up)
        {
            keyup = false;
        }
        else if (e.KeyCode == Keys.Left)
        {
            keyleft = false;
        }
    }
}