Search code examples
c#wpfkey-bindingsreactiveui

How to set up KeyBindings the reactiveui way?


I'd like to know how to set up KeyBindings (Shortcuts) in the reactiveui way. I know about the reactiveui.events package and I'm able to use it for single key events or sequences as described in the handbook. But I'm stuck at using it for something like Ctrl+Up or anything like that.

I tried the handbook k-code example but that is for a sequence of key events and is not working for something like Ctrl+Up.


Solution

  • KeyUp returns an IObservable<KeyEventArgs> that you can compose as usual. Try this:

    this.Events().KeyUp
        .Where(x => x.Key == Key.Up && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
        .Subscribe(_ => MessageBox.Show("CTRL+Up detected!"));