Form1_KeyDown NOT WORKING

本文关键字:WORKING NOT KeyDown Form1 | 更新日期: 2023-09-27 18:14:26

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (listBox1.Items.Contains(e.KeyCode))
    {
        listBox1.Items.Remove(e.KeyCode);
        listBox1.Refresh();
        timer1.Interval -= 10;
        difficultyProgessbar.Value = 800 - timer1.Interval;
        stats.update(true);
    }
    else
    {
        stats.update(false);
    }
    correctLabel.Text = stats.correct.ToString();
    missedLabel.Text = stats.missed.ToString();
    totalLabel.Text = stats.total.ToString();
    accuracyLabel.Text = stats.accuracy.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
    //Add a random key to Listbox
    listBox1.Items.Add((Keys)random.Next(65, 90));
    Application.DoEvents();
    if (listBox1.Items.Count > 7)
    {
        listBox1.Items.Clear();
        listBox1.Items.Add("Game Over");
        timer1.Stop();
    }
}

当我运行我的应用程序时,timer1_Tick事件工作正常,但是当我按任何键时,Form1_KeyDown事件不执行。是不是少了什么?为什么Key_Down事件从不触发?由于

Form1_KeyDown NOT WORKING

在Control with Focus中按下键。
要在窗体级别接收它,您需要设置该属性。KeyPreview=True