NOTE: e.IsRepeat
is confirmed to work. The problem exists because I use Remote Desktop from Ubuntu to Windows.
I found a workaround for this Remote Desktop problem:
- Disable key repetition in Ubuntu.
- In host Windows: Enable FilterKeys with "Turn on Repeat Keys and Slow Keys"
- Using regedit go to
HKEY_CURRENT_USER\Control Panel\Accessibility\Keyboard Response
- Set
AutoRepeatDelay
,AutoRepeatRate
, andLast Valid Delay
,Last Valid Repeat
to small enough. - Disable FilterKeys and re-enable to refresh the registry changes.
- Set
How does one detect key repetition in KeyUp
/KeyDown
(or PreviewKeyDown
/PreviewKeyUp
) events?
I have 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);
}
}
It gives me to Output screen following if I press 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 the e.IsRepeat
is always false, so that is useless. I also noticed that sometimes the first event is also toggFalse, dowTrue, so that cannot be used as a pattern.
I also note that clever way of using timing can be used to detect repetition, but there must be a native way to do this.