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

后端 未结 3 1111
一向
一向 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:19

    Declare a boolean isKeyDown, in theKeyDown Event, set the boolean to true. In the KeyUp event, set the boolean to false.

    This is sample code

    bool isKeyDown=false;
    public void control_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if(isKeyDown)
          return;
        isKeyDown=true;
        // do what you want to do
    }
    
    public void control1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {
       isKeyDown=false;
       // do you key up event, if any. 
    }
    

提交回复
热议问题