我是新来C#我做了一些工作在一个现有的应用程序。我有一个安装视区,已经组成,我希望能够用键。

目前,我的首要ProcessCmdKey和捕箭头的输入输送OnKeyPress事件。这一工作,但我希望能够使用改性剂(ALT+CTRL+移位).尽快,我保持一个修改并按一个箭头没有事件被触发,我听。

没有任何人有任何想法或建议,我应该去吗?

有帮助吗?

解决方案

在你的复盖ProcessCmdKey你是如何确定哪些关键已按压?

值keyData(第二参数)的改变将取决于关键的压和任何修改键,因此,例如,按下箭将返回代码37、移左将返回65573,按ctrl-左131109和alt-左262181.

你可以抽取的改性剂和关键的压力下安定与适当的枚举,值:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    bool shiftPressed = (keyData & Keys.Shift) != 0;
    Keys unmodifiedKey = (keyData & Keys.KeyCode);

    // rest of code goes here
}

其他提示

我投赞成票 Tokabi的答案, ,但是对于比较键是有一些额外的意见 StackOverflow.com 在这里,.这里有一些功能,我用来帮助简化一切。

   public Keys UnmodifiedKey(Keys key)
    {
        return key & Keys.KeyCode;
    }

    public bool KeyPressed(Keys key, Keys test)
    {
        return UnmodifiedKey(key) == test;
    }

    public bool ModifierKeyPressed(Keys key, Keys test)
    {
        return (key & test) == test;
    }

    public bool ControlPressed(Keys key)
    {
        return ModifierKeyPressed(key, Keys.Control);
    }

    public bool AltPressed(Keys key)
    {
        return ModifierKeyPressed(key, Keys.Alt);
    }

    public bool ShiftPressed(Keys key)
    {
        return ModifierKeyPressed(key, Keys.Shift);
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (KeyPressed(keyData, Keys.Left) && AltPressed(keyData))
        {
            int n = code.Text.IndexOfPrev('<', code.SelectionStart);
            if (n < 0) return false;
            if (ShiftPressed(keyData))
            {
                code.ExpandSelectionLeftTo(n);
            }
            else
            {
                code.SelectionStart = n;
                code.SelectionLength = 0;
            }
            return true;
        }
        else if (KeyPressed(keyData, Keys.Right) && AltPressed(keyData))
        {
            if (ShiftPressed(keyData))
            {
                int n = code.Text.IndexOf('>', code.SelectionEnd() + 1);
                if (n < 0) return false;
                code.ExpandSelectionRightTo(n + 1);
            }
            else
            {
                int n = code.Text.IndexOf('<', code.SelectionStart + 1);
                if (n < 0) return false;
                code.SelectionStart = n;
                code.SelectionLength = 0;
            }
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top