Some time ago, it was a headache because of this problem that triggered multiple Keyboard Events at the same time,
Now,
List <T> generic set is used here.
First, a static set is used to store the set of keys that have been pressed, and then all Keyboard Events in the set are run. Of course, when you open a key, remove it from the keyboard set and run it again.
The following is a simple example.
Static List <char> pressChar = new List <char> ();
Clever Use of KeyPress and KeyUp events
Private void Form1_KeyUp (object sender, KeyEventArgs e)
{
PressChar. Remove (char) e. KeyCode );
Foreach (char c in pressChar)
{
Switch (c)
{
Case 'A': this. button1.Left-= 1;
Break;
Case 'D': this. button1.Left + = 1;
Break;
Case 'W': this. button1.Top-= 1;
Break;
Case's ': this. button1.Top + = 1;
Break;
}
}
}
Private void Form1_KeyPress (object sender, KeyPressEventArgs e)
{
If (! PressChar. Contains (e. KeyChar ))
{
Pressint. Add (e. KeyChar );
}
Foreach (char c in pressChar)
{
Switch (c)
{
Case 'A': this. button1.Left-= 1;
Break;
Case 'D': this. button1.Left + = 1;
Break;
Case 'W': This. button1.top-= 1;
Break;
Case's ': This. button1.top + = 1;
Break;
}
}
}
In this way, you can run all the events that are pressing keys .....