WPF中的多键手势

jaw*_*har 6 wpf hotkeys routed-commands

我有一个RoutedUICommandComment Selection. 我需要为此命令添加一个输入手势,就像在 VIsual Studio 中一样,即。( Ctrl+K, Ctrl+C)。我怎样才能做到这一点?请帮助我。(记住 VS 的功能)。

问候,贾瓦哈尔

Ser*_*kiy 5

此代码是为“Ctrl+W、Ctrl+E”和/或“Ctrl+W、E”组合编写的,但是您可以针对任何组合键对其进行参数化:

XAML:

<MenuItem Header="Header" InputGestureText="Ctrl+W, E" Command="ShowCommand"/>
Run Code Online (Sandbox Code Playgroud)

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Osk*_*kar 4

我发现这篇博客文章我认为可能会有帮助

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

基本上,WPF 没有对其的内置支持,但子类化 InputGesture 或 KeyGesture 似乎是实现此目的的一种可能方法,而无需太多麻烦。