简体   繁体   English

捕获 ctrl + 多个按键

[英]Capturing ctrl + multiple key downs

Is there an easy way to capture a ctrl + key1 , key2 event in a winforms app similar to that in visual studio such as ctrl + e , c = comment out selected lines?是否有一种简单的方法可以在类似于 Visual Studio 中的 winforms 应用程序中捕获ctrl + key1 , key2事件,例如ctrl + e , c = 注释掉选定的行?

I am currently overriding my forms OnKeyDown event:我目前正在覆盖我的表单OnKeyDown事件:

protected override void OnKeyDown(KeyEventArgs e)
    {
        base.OnKeyDown(e);

        if (e.Control && e.KeyCode.ToString() == "N")
        {
            //do something...
        }
    }

The article in the comment may have been for WPF but the idea in it can still be used评论里的文章可能是针对WPF的,但是里面的想法还是可以用的

Construct a class such as构造一个类,例如

    public class MultiKeyGesture
    {
        private List<Keys> _keys;
        private Keys _modifiers;
        public MultiKeyGesture(IEnumerable<Keys> keys, Keys modifiers)
        {
            _keys = new List<Keys>(keys);
            _modifiers = modifiers;
            if (_keys.Count == 0)
            {
                throw new ArgumentException("At least one key must be specified.", "keys");
            }
        }

        private int currentindex;
        public bool Matches(KeyEventArgs e)
        {
            if (e.Modifiers == _modifiers && _keys[currentindex] == e.KeyCode)
                //at least a partial match
                currentindex++;
            else
                //No Match
                currentindex = 0;
            if (currentindex + 1 > _keys.Count)
            {
                //Matched last key
                currentindex = 0;
                return true;
            }
            return false;
        }
    }

but ignore the inheritance.但忽略继承。

to use it使用它

    private MultiKeyGesture Shortcut1 = new MultiKeyGesture(new List<Keys> { Keys.A, Keys.B }, Keys.Control);
    private MultiKeyGesture Shortcut2 = new MultiKeyGesture(new List<Keys> { Keys.C, Keys.D }, Keys.Control);
    private MultiKeyGesture Shortcut3 = new MultiKeyGesture(new List<Keys> { Keys.E, Keys.F }, Keys.Control);

    protected override void OnKeyDown(KeyEventArgs e)
    {
        base.OnKeyDown(e);
        if (Shortcut1.Matches(e))
            BackColor = Color.Green;
        if (Shortcut2.Matches(e))
            BackColor = Color.Blue;
        if (Shortcut3.Matches(e))
            BackColor = Color.Red;
    }

If you only have a two key shortcut, like VS does, you could store the last key pressed in a variable.如果您只有两个快捷键,就像 VS 那样,您可以将最后按下的键存储在一个变量中。

private Keys lastKeyPressed = null;

protected override void OnKeyDown(KeyEventArgs e)
{
    base.OnKeyDown(e);
    if(e.Control && lastKeyPressed != null)
    {
        if(lastKeyPressed == Keys.firstKey && e.KeyCode == Keys.secondKey)
        {
        }
        else if (...) // so on and so forth.
    }
    else if(e.Control)
        lastKeyPressed = e.KeyCode;
}

protected override void OnKeyUp(KeyEventsArgs e)
{
    if(!e.Control)
       lastKeyPressed = null;
}

This would do a two key shortcut, and would reset it when the ctrl key is released.这将执行两个键的快捷方式,并在释放 ctrl 键时重置它。 This is just untested pseudo code but its the concept of saving the last pressed key when Ctrl is being held then resetting it when ctrl is released that I'm trying to convey.这只是未经测试的伪代码,但它的概念是在按住 Ctrl 时保存最后一个按下的键,然后在我试图传达的 ctrl 释放时重置它。

Call e.Modifiers for control key and e.KeyCode for the combined key.为控制键调用 e.Modifiers,为组合键调用 e.KeyCode。

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
   // For Tow Key Shortcut.
   if (e.Modifiers == Keys.Control && e.KeyCode == Keys.B)
   {}
   
     // For Triple Key Shortcut.
   if (e.Modifiers.ToString() == (Keys.Shift+", "+Keys.Control) && e.KeyCode == Keys.B)
   {} 
}

// For Form level Key events you must have to set KeyPreview to True;
public Form1()
{
    InitializeComponent();
    this.KeyPreview = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM