multi key gesture in wpf
Asked Answered
I

4

6

I have a RoutedUICommand called Comment Selection. I need to add an input gesture for this command as it is in VIsual Studio, ie. (Ctrl+K, Ctrl+C). How can I do this? Plz help me. (Keep VS functionality in mind).

Regards, Jawahar

Implacental answered 2/2, 2010 at 5:3 Comment(0)
H
5

This code is made for "Ctrl+W, Ctrl+E" and/or "Ctrl+W, E" combinations, however you can parametrize it for any key combinations:

XAML:

<MenuItem Header="Header" InputGestureText="Ctrl+W, E" Command="ShowCommand"/>

C#:

public static readonly RoutedUICommand ShowCommand = new RoutedUICommand(
    "Show command text", 
    "Show command desc", 
    typeof(ThisWindow), 
    new InputGestureCollection(new[] { new ShowCommandGesture (Key.E) }));

public class ShowCommandGesture : InputGesture
{
    private readonly Key _key;
    private bool _gotFirstGesture;
    private readonly InputGesture _ctrlWGesture = new KeyGesture(Key.W, ModifierKeys.Control);

    public ShowCommandGesture(Key key)
    {
        _key = key;
    }

    public override bool Matches(object obj, InputEventArgs inputEventArgs)
    {
        KeyEventArgs keyArgs = inputEventArgs as KeyEventArgs;
        if (keyArgs == null || keyArgs.IsRepeat)
            return false;

        if (_gotFirstGesture)
        {
            _gotFirstGesture = false;

            if (keyArgs.Key == _key)
            {
                inputEventArgs.Handled = true;
            }

            return keyArgs.Key == _key;
        }
        else
        {
            _gotFirstGesture = _ctrlWGesture.Matches(null, inputEventArgs);
            if (_gotFirstGesture)
            {
                inputEventArgs.Handled = true;
            }

            return false;
        }
    }
}
Hand answered 17/7, 2015 at 18:57 Comment(0)
C
4

I've found this blog post which I think could be of help

http://kent-boogaart.com/blog/multikeygesture

Basically, WPF has no built in support for it, but subclassing InputGesture or KeyGesture seems like a possible way to achieve this without too much hassle.

Clank answered 22/7, 2010 at 7:49 Comment(0)
B
1

Here's how I cobbled together something that actually works. I just wish I could credit the person or persons who paved the way to my Path of Enlightenment.

Let's say your application is called Heckler. Add a namespace tag for your application to the Window object:

<Window ...
    xmlns:w="clr-namespace:Heckler" 
    ...>

Now add a CommandBindings property tag and start your collection of CommandBinding objects. Here we add custom command Comment Selection:

<Window.CommandBindings>
    <CommandBinding
        Command="w:CustomCommands.CommentSelection"
        CanExecute="CommentSelectionCanExecute"
        Executed="CommentSelectionExecuted" />
</Window.CommandBindings>

Add a MenuItem to a main Menu's MenuItem:

    <Menu
        IsMainMenu="True">
        <MenuItem
            Header="_File">
            <MenuItem
                Command="w:CustomCommands.CommentSelection">
            </MenuItem>
        </MenuItem>
    </Menu>
    ...
</Window>

In the Window code-behind, add your CustomCommands class and custom command:

public static class CustomCommands
{
    // Ctrl+Shift+C to avoid collision with Ctrl+C.
    public static readonly RoutedUICommand CommentSelection = 
        new RoutedUICommand("_Comment Selection", 
            "CommentSelection", typeof(MainWindow), 
            new InputGestureCollection() 
            { new KeyGesture(Key.C, (ModifierKeys.Control | ModifierKeys.Shift)) });
}

Now wire up your event handlers:

private void CommentSelectionCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    // Determines status of command.
    e.CanExecute = true;
}

private void CommentSelectionExecuted(object sender, ExecutedRoutedEventArgs e)
{
    // TO-DO: Insert magic here.
}

You should be good to go. I hope this helps and I didn't miss anything!

Bromide answered 12/12, 2012 at 18:38 Comment(0)
A
-1
<KeyBinding Command="{Binding ExitCommand}"

                Key="{Binding ExitCommand.GestureKey}"

                Modifiers="{Binding ExitCommand.GestureModifier}"/>
get

    {

        if (exitCommand == null)

        {

            exitCommand = new DelegateCommand(Exit);

            exitCommand.GestureKey = Key.X;

            exitCommand.GestureModifier = ModifierKeys.Control;

            exitCommand.MouseGesture = MouseAction.LeftDoubleClick;

        }

        return exitCommand;

    }

}
 private void Exit()
{
    Application.Current.Shutdown();
}
Annihilation answered 3/10, 2012 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.