当按下一个键,并举行确保语句只执行一次。 [英] Ensure statement is executed only once when a key is pressed and held.

查看:299
本文介绍了当按下一个键,并举行确保语句只执行一次。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您按下并按住数字键盘,将继续在KeyDown事件处理程序来执行语句5键。我怎么能保证语句只执行一次,即使我按住键?



感谢您的关注。

 私人无效form_KeyDown(对象发件人,System.Windows.Forms.KeyEventArgs E)
{
如果(e.KeyCode == Keys.NumPad5)
{
dados.enviar(F); //我想这一次跑!
}
}


解决方案

您,可以对关键设置的标志下,并重新设置关键了。

 私人布尔isPressed = FALSE; 
公共Form1中()
{
的InitializeComponent();
}

私人无效Form1_KeyDown(对象发件人,发送KeyEventArgs E)
{
如果(e.KeyCode == Keys.B&放大器;&安培;!isPressed)
{
isPressed = TRUE;
//做的工作
}
}

私人无效Form1_KeyUp(对象发件人,发送KeyEventArgs E)
{
如果(isPressed)
isPressed = FALSE;
}


If you press and hold the 5 key on the numpad it will continue to execute a statement in the KeyDown event handler. How can i ensure the statement is executed only once, even if i hold the key down?

Thanks for your attention.

private void form_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
   if (e.KeyCode == Keys.NumPad5)
   {
        dados.enviar("f"); //I want this to run only once!
   }
}

解决方案

You can set flag on key down and reset it on key up.

    private bool isPressed = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if(e.KeyCode == Keys.B && !isPressed )
        {
            isPressed = true;
            // do work
        }
    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        if (isPressed )
            isPressed = false;
    }

这篇关于当按下一个键,并举行确保语句只执行一次。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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