CTRL后任意整数组合的关键手势

时间:2015-04-11 23:18:17

标签: c# wpf

我在C#中使用关键手势作为我的应用程序的热键 我知道怎么做 ctrl + shift + s

new KeyGesture(Key.S, ModifierKeys.Shift | ModifierKeys.Control)

但任何人都能告诉我如何为 ctrl + e + (1 t0 50中的任何整数)

1 个答案:

答案 0 :(得分:1)

一个简单的KeyGesture不允许你进行多键击中,但你可以扩展KeyGesture类来支持这一点。现在数字0到9很容易,但是在上面这很复杂,因为你需要考虑的是9以上的数字成为两个关键的stokes(基本上是3个包括 e 字符)。

这是一个允许多个键绑定的示例类(注意我没有写这个并且不能记住我得到它的地方)。

KeyGesture类:

[TypeConverter(typeof(MultiKeyGestureConverter))]
public class MultiKeyGesture : KeyGesture
{
    private readonly IList<Key> _keys;
    private readonly ReadOnlyCollection<Key> _readOnlyKeys;
    private int _currentKeyIndex;
    private DateTime _lastKeyPress;
    private static readonly TimeSpan _maximumDelayBetweenKeyPresses = TimeSpan.FromSeconds(1);

    public MultiKeyGesture(IEnumerable<Key> keys, ModifierKeys modifiers)
        : this(keys, modifiers, string.Empty)
    {
    }

    public MultiKeyGesture(IEnumerable<Key> keys, ModifierKeys modifiers, string displayString)
        : base(Key.None, modifiers, displayString)
    {
        _keys = new List<Key>(keys);
        _readOnlyKeys = new ReadOnlyCollection<Key>(_keys);

        if (_keys.Count == 0)
        {
            throw new ArgumentException("At least one key must be specified.", "keys");
        }
    }

    public ICollection<Key> Keys
    {
        get { return _readOnlyKeys; }
    }

    public override bool Matches(object targetElement, InputEventArgs inputEventArgs)
    {
        var args = inputEventArgs as KeyEventArgs;

        if ((args == null) || !IsDefinedKey(args.Key))
        {
            return false;
        }

        if (_currentKeyIndex != 0 && ((DateTime.Now - _lastKeyPress) > _maximumDelayBetweenKeyPresses))
        {
            //took too long to press next key so reset
            _currentKeyIndex = 0;
            return false;
        }

        //the modifier only needs to be held down for the first keystroke, but you could also require that the modifier be held down for every keystroke
        if (_currentKeyIndex == 0 && Modifiers != Keyboard.Modifiers)
        {
            //wrong modifiers
            _currentKeyIndex = 0;
            return false;
        }

        if (_keys[_currentKeyIndex] != args.Key)
        {
            //wrong key
            _currentKeyIndex = 0;
            return false;
        }

        ++_currentKeyIndex;

        if (_currentKeyIndex != _keys.Count)
        {
            //still matching
            _lastKeyPress = DateTime.Now;
            inputEventArgs.Handled = true;
            return false;
        }

        //match complete
        _currentKeyIndex = 0;
        return true;
    }

    private static bool IsDefinedKey(Key key)
    {
        return ((key >= Key.None) && (key <= Key.OemClear));
    }
}

类型转换器:

public class MultiKeyGestureConverter : TypeConverter
{
    private readonly KeyConverter _keyConverter;
    private readonly ModifierKeysConverter _modifierKeysConverter;

    public MultiKeyGestureConverter()
    {
        _keyConverter = new KeyConverter();
        _modifierKeysConverter = new ModifierKeysConverter();
    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(string);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        var keyStrokes = (value as string).Split(',');
        var firstKeyStroke = keyStrokes[0];
        var firstKeyStrokeParts = firstKeyStroke.Split('+');

        var modifierKeys = (ModifierKeys)_modifierKeysConverter.ConvertFrom(firstKeyStrokeParts[0]);
        var keys = new List<Key>();

        keys.Add((Key)_keyConverter.ConvertFrom(firstKeyStrokeParts[1]));

        for (var i = 1; i < keyStrokes.Length; ++i)
        {
            keys.Add((Key)_keyConverter.ConvertFrom(keyStrokes[i]));
        }

        return new MultiKeyGesture(keys, modifierKeys);
    }
}

现在这个类很简单,它允许多个键与附加修饰符绑定。然后可以将其用作。

this.InputBindings.Add(new InputBinding(MyCommand, new MultiKeyGesture(new Key[] { Key.E, Key.D1 }, ModifierKeys.Control, "Ctrl+E, 1")));

这将命令MyCommand绑定到键命令 ctrl + e + 1

现在将数字 0 绑定到 9 很简单,但是当用户必须输入两个数字(如10)时,可能会遇到问题: ctrl + e + 1 + 0