我可以创建在WPF键序列的键绑定? [英] Can I create a KeyBinding for a sequence of keys in WPF?

查看:97
本文介绍了我可以创建在WPF键序列的键绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在WPF定义键绑定关键presses像在Visual Studio例如快捷键序列<大骨节病>控制 + <大骨节病>研究,<大骨节病>控制 + <大骨节病> A 运行在当前解决方案中的所有测试

Is it possible to define key bindings in WPF for a sequence of key presses like the shortcuts in Visual Studio e.g. Ctrl + R, Ctrl + A is run all tests in current solution

据我可以看到我只能绑定如单组合键<大骨节病>控制 + <大骨节病>取值使用元素。我可以绑定使用此序列或我将不得不手动处理的关键presses做到这一点?

As far as I can see I can only bind single key combinations like Ctrl + S using the element. Can I bind sequences using this or will I have to manually handle the key presses to do this?

推荐答案

您需要创建自己的 InputGesture ,通过重写的 匹配 方法。

You need to create your own InputGesture, by overriding the Matches method.

类似的东西:

public class MultiInputGesture : InputGesture
{
    public MultiInputGesture()
    {
        Gestures = new InputGestureCollection();
    }

    public InputGestureCollection Gestures { get; private set; }

    private int _currentMatchIndex = 0;

    public override bool Matches(object targetElement, InputEventArgs inputEventArgs)
    {
        if (_currentMatchIndex < Gestures.Count)
        {
            if (Gestures[_currentMatchIndex].Matches(targetElement, inputEventArgs))
            {
                _currentMatchIndex++;
                return (_currentMatchIndex == Gestures.Count);
            }
        }
        _currentMatchIndex = 0;
        return false;
    }
}

这可能需要不止一点点,就像忽略某些事件(如的KeyUp 的KeyDown 之间的事件事件不该T重置 _currentMatchIndex ),但你得到的图片...

It probably needs a little more than that, like ignoring certain events (e.g. KeyUp events between KeyDown events shouldn't reset _currentMatchIndex), but you get the picture...

这篇关于我可以创建在WPF键序列的键绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆