how not to allow multiple keystokes received at one key press?

后端 未结 3 1091
一向
一向 2020-12-20 20:31

when we press a key and keep pressing it the keypress and keydown event continuously fires. Is there a way to let these fire only after a complete cycle ,eg keydown and the

3条回答
  •  生来不讨喜
    2020-12-20 21:08

    To prevent a key from firing multiple-times when held down : you must use the SuppressKeyPress property like so :

    bool isKeyRepeating = false;
    
    public void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (isKeyRepeating)
        {
            e.SuppressKeyPress = true;
        }
        else
        {
            isKeyRepeating = true;
        }
    
    }
    
    public void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        isKeyRepeating = false;
    }
    

    See : KeyEventArgs..::.Handled Property ... and ... KeyEventArgs..::.SuppressKeyPress Property .... for relevant information

提交回复
热议问题