1
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();
    }
}

When I run my application, timer1_Tick event is working fine, however Form1_KeyDown event doesn't execute when I press any key. Is something missing? Why Key_Down event never fires? Thanks

Artemix
  • 2,113
  • 2
  • 23
  • 34
Varun Kumar
  • 478
  • 1
  • 4
  • 15

1 Answers1

5

Keydown fires in the Control with Focus.
To receive it at the Form level you need to set the property. KeyPreview=True for the Form

Steve
  • 213,761
  • 22
  • 232
  • 286