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

NOTE : e.IsRepeat verified to be working. The problem exists because I use Remote Desktop from Ubuntu to Windows.

I found a workaround for this remote desktop issue:

  • Disable key repetition in Ubuntu.
  • On a Windows host: Enable FilterKeys with "Enable repeat keys and slow keys"
  • Use regedit to go to HKEY_CURRENT_USER\Control Panel\Accessibility\Keyboard Response
    • Set AutoRepeatDelay , AutoRepeatRate and Last Valid Delay , Last Valid Repeat quite small.
    • Disable FilterKeys and enable it again to update registry changes.

How to detect duplicate keywords in KeyUp / KeyDown (or PreviewKeyDown / PreviewKeyUp ) PreviewKeyUp ?

I have the following test case:

  public Window1() { InitializeComponent(); this.KeyDown += new KeyEventHandler(Window1_KeyDown); this.KeyUp += new KeyEventHandler(Window1_KeyUp); } void Window1_KeyUp(object sender, KeyEventArgs e) { if (e.Key == Key.D) { Console.WriteLine("DOWN: key: {0}, rep{1}, togg{2}, dow{3}, up{4}", e.Key, e.IsRepeat, e.IsToggled, e.IsDown, e.IsUp); } } void Window1_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.D) { Console.WriteLine("UP: key: {0}, rep{1}, togg{2}, dow{3}, up{4}", e.Key, e.IsRepeat, e.IsToggled, e.IsDown, e.IsUp); } } 

This gives me an Output Screen , the following if I press the letter D down and release it after a while:

 // Note: Here I press D-key down. UP: key: D, repFalse, toggTrue, dowTrue, upFalse DOWN: key: D, repFalse, toggTrue, dowFalse, upTrue UP: key: D, repFalse, toggFalse, dowTrue, upFalse DOWN: key: D, repFalse, toggFalse, dowFalse, upTrue UP: key: D, repFalse, toggTrue, dowTrue, upFalse DOWN: key: D, repFalse, toggTrue, dowFalse, upTrue UP: key: D, repFalse, toggFalse, dowTrue, upFalse DOWN: key: D, repFalse, toggFalse, dowFalse, upTrue UP: key: D, repFalse, toggTrue, dowTrue, upFalse DOWN: key: D, repFalse, toggTrue, dowFalse, upTrue UP: key: D, repFalse, toggFalse, dowTrue, upFalse DOWN: key: D, repFalse, toggFalse, dowFalse, upTrue UP: key: D, repFalse, toggTrue, dowTrue, upFalse DOWN: key: D, repFalse, toggTrue, dowFalse, upTrue UP: key: D, repFalse, toggFalse, dowTrue, upFalse DOWN: key: D, repFalse, toggFalse, dowFalse, upTrue UP: key: D, repFalse, toggTrue, dowTrue, upFalse DOWN: key: D, repFalse, toggTrue, dowFalse, upTrue UP: key: D, repFalse, toggFalse, dowTrue, upFalse DOWN: key: D, repFalse, toggFalse, dowFalse, upTrue UP: key: D, repFalse, toggTrue, dowTrue, upFalse DOWN: key: D, repFalse, toggTrue, dowFalse, upTrue UP: key: D, repFalse, toggFalse, dowTrue, upFalse DOWN: key: D, repFalse, toggFalse, dowFalse, upTrue // Note: Here I release D-key. 

Apparently e.IsRepeat always a lie, therefore useless. I also noticed that sometimes the first event is also toggFalse, dowTrue, so it cannot be used as a template.

I also note that a smart way to use synchronization can be used to detect repetition, but there must be a custom way to do this.

+6
c # wpf keyboard-events
source share
2 answers

Why not use your own capabilities? I added a PreviewKeyDown event in the window and two text fields. Press and hold the key in the second text box, and this will be the output:

 Repeat: False, key: D Repeat: True, key: D Repeat: True, key: D Repeat: True, key: D Repeat: True, key: D Repeat: True, key: D Repeat: True, key: D Repeat: 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 paste it into the code as is. This gives me the following output in the console, so I understand we have to look at other reasons ...

 UP: key: D, repFalse, toggTrue, dowTrue, upFalse UP: key: D, repTrue, toggTrue, dowTrue, upFalse UP: key: D, repTrue, toggTrue, dowTrue, upFalse UP: key: D, repTrue, toggTrue, dowTrue, upFalse UP: key: D, repTrue, toggTrue, dowTrue, upFalse UP: key: D, repTrue, toggTrue, dowTrue, upFalse 
+7
source share

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

0
source share

All Articles