1

I don't understand why it works with one modifiers and one key, but not one modifier and multiple keys (when Ctr+R+S+V are pressed all together). And if there is any workaround?

if ((Keyboard.Modifiers == ModifierKeys.Control) 
      && (e.Key == Key.R) 
      && (e.Key == Key.S) 
      && (e.Key == Key.V))
{ ... }
KMC
  • 19,548
  • 58
  • 164
  • 253
  • 1
    You mean you want to catch CTRL+R+S+V, all pressed together? – Evk Mar 17 '17 at 10:25
  • 1
    Key is one variable, it cant be equal to 3 different values at the same time – Arsen Mkrtchyan Mar 17 '17 at 10:28
  • http://stackoverflow.com/a/19013440/6240567 - this might help - @ArsenMkrtchyan has hit the nail on the head, I think. This answer shows how to detect *multiple* keys pressed at the same time :) EDIT: Evk's answer below (posted at the same time as this comment) is virtually the same :) – Geoff James Mar 17 '17 at 10:32

1 Answers1

3

Key enumeration is not marked with Flags and so cannot hold multiple values. And there is just one Key property in that event args, so just one key. Hence, your if can never be true because 3 of your && conditions are mutually exclusive.

What you can do instead is this:

if ((Keyboard.Modifiers == ModifierKeys.Control)
                && (Keyboard.IsKeyDown(Key.R))
                && (Keyboard.IsKeyDown(Key.S))
                && (Keyboard.IsKeyDown(Key.V))) {

}

Note that if you want to allow other modifier keys to be pressed at the same time (so, if you don't care if both ALT and CONTROL might be pressed together for example), then you should use

Keyboard.Modifiers.HasFlag(ModifierKeys.Control)

instead.

Evk
  • 98,527
  • 8
  • 141
  • 191