简体   繁体   English

C#和箭头键

[英]C# and Arrow Keys

I am new to C# and am doing some work in an existing application. 我是C#的新手,我正在现有的应用程序中做一些工作。 I have a DirectX viewport that has components in it that I want to be able to position using arrow keys. 我有一个DirectX视口,其中包含我希望能够使用箭头键定位的组件。

Currently I am overriding ProcessCmdKey and catching arrow input and send an OnKeyPress event. 目前我正在重写ProcessCmdKey并捕获箭头输入并发送OnKeyPress事件。 This works, but I want to be able to use modifiers( ALT + CTRL + SHIFT ). 这有效,但我希望能够使用修饰符( ALT + CTRL + SHIFT )。 As soon as I am holding a modifier and press an arrow no events are triggered that I am listening to. 一旦我拿着修改器并按箭头,就不会触发我正在听的事件。

Does anyone have any ideas or suggestions on where I should go with this? 有没有人对我应该去哪里有任何想法或建议?

Within your overridden ProcessCmdKey how are you determining which key has been pressed? 在重写的ProcessCmdKey中,您如何确定已按下哪个键?

The value of keyData (the second parameter) will change dependant on the key pressed and any modifier keys, so, for example, pressing the left arrow will return code 37, shift-left will return 65573, ctrl-left 131109 and alt-left 262181. keyData(第二个参数)的值将根据按下的键和任何修改键而改变,因此,例如,按向左箭头将返回代码37,shift-left将返回65573,ctrl-left 131109和alt-left 262181。

You can extract the modifiers and the key pressed by ANDing with appropriate enum values: 您可以使用适当的枚举值提取修饰符和ANDing按下的键:

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
}

I upvoted Tokabi's answer , but for comparing keys there is some additional advice on StackOverflow.com here . 我赞成了Tokabi的答案 ,但是为了比较键,这里有一些关于StackOverflow.com的额外建议。 Here are some functions which I used to help simplify everything. 以下是我用来帮助简化一切的一些功能。

   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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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