Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

KeyGesture bound to RoutedUICommand not working with Key.Left but working with other keys

$
0
0

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 were to replace the Key.NumPad4 above (which is the left arrow on the numeric keypad) with Key.Left, or any of the dedicated arrow keys (i.e., not on the numeric keypad), 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. The keyboard/driver/etc. are all standard. 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 input gestures on a command. And it's not like it isn't working for me at all; it is only 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.




Viewing all articles
Browse latest Browse all 18858

Trending Articles