Question

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 )
        }
    );
}
Was it helpful?

Solution 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;
                }
            }
        }
    }
}

OTHER TIPS

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);
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top