是否有一种方法可以在不使用事件的情况下检查键盘输入?

时间:2017-01-07 20:54:26

标签: c# winforms input visual-studio-2015

我正在为个人项目创建游戏,而且我正在使用Windows Form。如果用户点击箭头键,我想每隔几毫秒检查一次。我尝试过Keyboard.IsKeyDown()但它似乎在Windows窗体中不可用。那么有另一种方法吗?

public Form1()
    {
        InitializeComponent();
        fps = new Timer();
        fps.Interval = 30;
        fps.Tick += Fps_Tick;
        fps.Start();
    }

    private void Fps_Tick(object sender, EventArgs e)
    {
        int X = playerBlue.Location.X;
        int Y = playerBlue.Location.Y;

        if (((KeyEventArgs)e).KeyCode == Keys.Right)
        {
            X += 2;
            playerBlue.Location = new Point(X, Y);
        }

        if (((KeyEventArgs)e).KeyCode == Keys.Left)
        {
            X -= 2;
            playerBlue.Location = new Point(X, Y);
        }
    }

0 个答案:

没有答案