2

I have created a RoutedUICommand for pasting the plain text from the clipboard. If I change the Key.V assignment to Key.G or something else it works. I assume that the command is being intercepted by the current control (a RichTextBox). I tried to verify this with Snoop. However, I don't appear to be smart enough to find the culprit. Has anyone else seen this behavior? Is there a workaround?

Is there a way to find out what KeyGestures may already be assigned within a window or control?

// I REALLY want to use CTRL + SHIFT + V here but it seems to be ignored. 
public static readonly RoutedUICommand PasteUnformattedText = new RoutedUICommand
    (
        "Paste Text Only",
        "PasteUnformattedText",
        typeof(CustomCommands),
        new InputGestureCollection() 
        { 
            new KeyGesture(Key.V, ModifierKeys.Control | ModifierKeys.Shift )
        }
    );
}
Sheridan
  • 68,826
  • 24
  • 143
  • 183
Ward
  • 116
  • 1
  • 9
  • Try this I am not sure it will help you or not `public MainWindow() { InitializeComponent(); CommandManager.RegisterClassInputBinding(typeof(UIElement), new InputBinding(ApplicationCommands.NotACommand, new KeyGesture(Key.V, ModifierKeys.Control))); }` – Sameer Jan 26 '14 at 05:10
  • I did try something similar to that earlier. I modified your suggestion to included the Shift modifier key as well. Neither works. The code you suggest removed the KeyGesture for Paste (CTRL+V). – Ward Jan 26 '14 at 06:14

3 Answers3

3

To add to @shahrooz-jafari 's answer, you can use multiple keybindings with WPF Key Gestures.

public RoutedCommand myCommand = new RoutedCommand();
public void Init() {
    myCommand.InputGestures.Add(new KeyGesture(Key.V, ModifierKeys.Control | ModifierKeys.Shift);
    var bind = new CommandBinding { Command = myCommand };
    bind.Executed += new ExecutedRoutedEventHandler((sender,e) => {
        // do stuff here.
    });
    CommandBindings.Add(bind);
}
CodeMonkey
  • 419
  • 2
  • 10
2

Thanks to OxA3's question and his own answer, I found a workaround. Using the Preview_KeyDown event on the window has the desired effect. The workaround just pushes all of the KeyGestures to the window.

This was answered back in 2009. I researched for a long time and did not find it until today. I wanted to post the answer here so that anyone that was looking for something a little more specific in regard to KeyGestures would find it.

I preempted the foreach loops by adding a condition on the Keyboard.Modifiers state. Below are my changes:

void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
    // don't bother with it if we are not modified
    if (Keyboard.Modifiers == ModifierKeys.None) return;

    foreach (InputBinding inputBinding in this.InputBindings)
    {
        KeyGesture keyGesture = inputBinding.Gesture as KeyGesture;
        if (keyGesture != null && keyGesture.Key == e.Key && keyGesture.Modifiers == Keyboard.Modifiers)
        {
            if (inputBinding.Command != null)
            {
                inputBinding.Command.Execute(0);
                e.Handled = true;
            }
        }
    }

    foreach (CommandBinding cb in this.CommandBindings)
    {
        RoutedCommand command = cb.Command as RoutedCommand;
        if (command != null)
        {
            foreach (InputGesture inputGesture in command.InputGestures)
            {
                KeyGesture keyGesture = inputGesture as KeyGesture;
                if (keyGesture != null && keyGesture.Key == e.Key && keyGesture.Modifiers == Keyboard.Modifiers)
                {
                    command.Execute(0, this);
                    e.Handled = true;
                }
            }
        }
    }
}
Community
  • 1
  • 1
Ward
  • 116
  • 1
  • 9
-1

See this article.

WPF's built-in KeyGesture class is an InputGesture subclass that recognises a gesture based on keyboard input. The problem is, its definition of "gesture" is a single keypress. What I'd like to do is treat multiple keypresses as a single gesture, and KeyGesture does not support that.

  • 1
    This is still a single keypress. The letter V with two modifiers: Control and Shift. The code works just fine if I replace the letter V with a G. – Ward Jan 26 '14 at 04:09