tags:

views:

2974

answers:

3
+5  Q: 

C# and Arrow Keys

I am new to C# and am doing some work in an existing application. I have a DirectX viewport that has components in it that I want to be able to position using arrow keys.

Currently I am overriding ProcessCmdKey and catching arrow input and send an OnKeyPress event. This works, but I want to be able to use modifiers(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?

+1  A: 

I'm not sure if this is any good because I'm no good at c#, but here goes: http://geekswithblogs.net/clingermangw/articles/84666.aspx. Its from an XNA tutorial so it may be good.

Ed
+6  A: 

Within your overridden ProcessCmdKey how are you determining which key has been pressed?

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.

You can extract the modifiers and the key pressed by ANDing with appropriate enum values:

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
+2  A: 

I upvoted Tokabi's answer, but for comparing keys there is some additional advice on StackOverflow.com here. 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);
    }
cdiggins
Plus one million to you for writing readable code!
skypecakes