views:

458

answers:

2

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:

  1. Disable key repetition in Ubuntu.
  2. In host Windows: Enable FilterKeys with "Turn on Repeat Keys and Slow Keys"
  3. Using regedit go to HKEY_CURRENT_USER\Control Panel\Accessibility\Keyboard Response
    1. Set AutoRepeatDelay, AutoRepeatRate, and Last Valid Delay, Last Valid Repeat to small enough.
    2. Disable FilterKeys and re-enable to refresh the registry changes.


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.

A: 

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.

Mike B
Umh, right, see that the KeyUp is fired *always* also, so that wont work naturally.
Ciantic
But the way you described would work if clever timing would be used, but that is just insane... As I noted in question there must be other way to detect key repetition than stack closely timed keyUp/KeyDown events together.
Ciantic
No need for clever timing, just use the `IsRepeat` in the correct event, it's all there.
Abel
Sorry I am fairly new to WPF, so if you press and hold a key the keyup event fires? That seems to be a major issue!
Mike B
+2  A: 
Abel
What is the correct event? As I said if I change the events to Preview* it fails on my computer to set e.IsRepeat also.
Ciantic
Noticed the difference: none. I tried what you tried (but only after writing this post). I use .NET 3.5 SP1 with a normal WPF project. Added a window and then your code.
Abel
Just tested without textbox. Works. Then tested with your code. Works. Hmm... .NET not up-to-date, keylogger on your system?
Abel
I'll have to study my system more deeply, it would be nice to get more results from people about this. I have Windows 7 RC1 and 3.5SP1 (SP1 I assume, 3.5 for sure)... And I am connected through Remote Desktop from Ubuntu.
Ciantic
Ah wait! Remote Desktop might be playing you parts here! The client system (Ubuntu) has to send the key events through the RD client to the Win7 server. Try the same code running from the console (i.e., sitting at the actual computer) and you'll likely find it working again.
Abel