I have just started adding keyboard support for my application. It already had multiple commands implemented but they are all bound to controls, like menu items. Most UI elements in my application are custom controls. I define routed UI commands, like this:
public static RoutedUICommand LeftAlignProp { get { return LeftAlignCmd; } } private static RoutedUICommand LeftAlignCmd = new RoutedUICommand("Align selection left", "LeftAlign", typeof(ProgramListingSectionT));
Then, in the constructor I create the bindings:
CommandBindings.Add(new CommandBinding(LeftAlignProp, LeftAlignExecute, LeftAlignCanExecute)); LeftAlignProp.InputGestures.Add(new KeyGesture(Key.NumPad4, ModifierKeys.Control)); // Ctrl+LeftArrow
The handlers are pretty standard, too:
private void LeftAlignExecute(object sender, ExecutedRoutedEventArgs e) // Carry the actions of the "left align" command { LeftAlignSelection(); } private void LeftAlignCanExecute(object sender, CanExecuteRoutedEventArgs e) // Allow copying by default (may be overridden) { e.CanExecute = SelectedCodeEnvelopes.Count > 0; // Only when there is a selection }
I don't know if any of that matters because everything works - the above code works fine, I also have other commands (like Ctrl+C for "copy", etc.) My problem is that if I weer to replace the Key.NumPad4 above (which is the left arrow on the numeric keypad) with Key.Left, the command doesn't fire.
Nothing weird is going on, i.e., there is nothing else handling keyboard events (that I intended), much less specifically the arrow keys. I even tried handling the OnKeyDown and the related events to try and figure it out but those don't seem to fire at all. But it shouldn't matter, it's not like it's not working for me; it is only the left arrow key (and I suspect all the arrow keys); all other keys I've tried work fine. What a mystery!
Any ideas?
Kamen
Currently using Visual Studio 2010 SP1, native C++ (Windows API) and C# (.Net, WPF), on Windows 7 64-bit; Mountain Time zone.