Form1_KeyDown 不工作 [英] Form1_KeyDown NOT WORKING

查看:29
本文介绍了Form1_KeyDown 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 事件永远不会触发?谢谢

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

推荐答案

Keydown 在 Control with Focus 中触发.
要在表单级别接收它,您需要设置属性.KeyPreview=True 用于表单

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

这篇关于Form1_KeyDown 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆