Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

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

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 then key up.

I would like the user not to be able press the key continuously rather would like the user have to press then release the key board to type a character !

so that following case do not occur eg : pppppppppppppppppppppppp when user presses 'p' for 1 sec.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...