1

是否有一种简单的方法可以在 Winforms 应用程序中捕获类似于 Visual Studio 中的事件,例如+ ,ctrl=key1注释掉选定的行?key2ctrlec

我目前正在覆盖我的表单OnKeyDown事件:

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

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

3 回答 3

4

评论中的文章可能是针对 WPF 的,但里面的想法仍然可以使用

构造一个类,例如

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

但忽略继承。

使用它

    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;
    }
于 2012-06-22T15:31:14.540 回答
2

如果你只有两个快捷键,就像 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;
}

这将执行一个两键快捷方式,并在释放 ctrl 键时将其重置。这只是未经测试的伪代码,但它的概念是在按住 Ctrl 时保存最后按下的键,然后在释放我试图传达的 ctrl 时重置它。

于 2012-06-22T14:27:40.810 回答
0

为控制键调用 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;
}
于 2020-10-18T04:04:38.303 回答