WPF: How to detect Key repetition, in Key* events? WPF: How to detect Key repetition, in Key* events? wpf wpf

WPF: How to detect Key repetition, in Key* events?


Why not use the native possibilities? I added a PreviewKeyDown event on the window and two textboxes. Pressed and held a key in the second textbox and this is the output:

Repeat: False, key: DRepeat: True, key: DRepeat: True, key: DRepeat: True, key: DRepeat: True, key: DRepeat: True, key: DRepeat: True, key: DRepeat: True, key: D

This is the code I used:

private void Grid_PreviewKeyDown(object sender, KeyEventArgs e){    textBox1.Text += String.Format(        "Repeat: {0}, key: {1}\n",         e.IsRepeat,         e.Key);}

Update: removed all my code (there was some garbage from other tests) and pasted in your code as-is. It gives me the following output in the console, so I gather we should have a look at other causes...

UP: key: D, repFalse, toggTrue, dowTrue, upFalseUP: key: D, repTrue, toggTrue, dowTrue, upFalseUP: key: D, repTrue, toggTrue, dowTrue, upFalseUP: key: D, repTrue, toggTrue, dowTrue, upFalseUP: key: D, repTrue, toggTrue, dowTrue, upFalseUP: key: D, repTrue, toggTrue, dowTrue, upFalse


Set a variable when the keydown event fires tracking which key is pressed, do your thing(tm) then ignore further events for that key. When keyup fires clear the variable. You may need a list to track multiple keys.