1

I'd like to bind multiple keys to one command like this:

SwitchViewCommand.InputGestures.Add(new KeyGesture(Key.F7));
SwitchViewCommand.InputGestures.Add(new KeyGesture(Key.F8));

next in the event handler I'd like to switch views based on the key that was pressed:

private void SwitchViewCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
     // if F7 then do something, if F8 then do something else
}

but I cannot find a way to determine the key. I checked the event args in the debugger but I did not see anything useful. Is it at all possible or do I need to create a separate command for each case?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
t3chb0t
  • 16,340
  • 13
  • 78
  • 118
  • Have you tried to see the type and contents/properties of e.Command on your debugger? It should be the SwitchViewCommand you are using and you should be able to find the gesture there. Or maybe in e.Parameter property. Don't have a chance to test it, sorry. – M. Mennan Kara Aug 24 '12 at 16:33
  • Yes, it's null. That's why I'm asking. I had hoped it would be put in the e.Parameter property but this is not the case. – t3chb0t Aug 24 '12 at 16:38
  • Sorry, edited my comment, what about e.Command? Maybe the last item in InputGestures. – M. Mennan Kara Aug 24 '12 at 16:39
  • It contains only a general information about the command itself, like its name and a collection of gestures. Unfortunately none of them is marked in any way. – t3chb0t Aug 24 '12 at 16:42
  • What about something like this: `((KeyGesture) ((RoutedCommand)e.Command).InputGestures.Last()).Key` ? – M. Mennan Kara Aug 24 '12 at 16:44
  • Nope, does not work either. InputGestures is only a collection of assigned gestures so getting the last item is pointless because the collection does not change and in my case I always get F8. – t3chb0t Aug 24 '12 at 17:01
  • I found a solution like this one: http://stackoverflow.com/questions/2437289/wpf-key-binding-can-i-create-a-single-key-binding-that-fires-a-command-every-ti but it seems to me way to complex for such a simple thing. – t3chb0t Aug 24 '12 at 17:02
  • The simple thing is to use multiple commands. Why do you want to use the same command for different command actions? – Tergiver Aug 24 '12 at 18:15
  • Because they do not do anything that different that multiple commands would be suitable, they just should exchange one user control. Besides, why is it at all possible to assign multiple key combinations to a command when one cannot differentiate which one was pressed? – t3chb0t Aug 24 '12 at 18:53

0 Answers0