Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

hotkeys - multi key gesture in wpf

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...