<p>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.</p>

<p>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(<strong><kbd>ALT</kbd>+<kbd>CTRL</kbd>+<kbd>SHIFT</kbd></strong>). As soon as I am holding a modifier and press an arrow no events are triggered that I am listening to.</p>

<p>Does anyone have any ideas or suggestions on where I should go with this?</p>

[{'answer_id': 5026, 'author': 'Tokabi', 'author_id': 315, 'author_profile': 'https://Stackoverflow.com/users/315', 'pm_score': 5, 'selected': True, 'text': '<p>Within your overridden ProcessCmdKey how are you determining which key has been pressed?</p>\n\n<p>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.</p>\n\n<p>You can extract the modifiers and the key pressed by ANDing with appropriate enum values:</p>\n\n<pre><code>protected override bool ProcessCmdKey(ref Message msg, Keys keyData)\n{\n    bool shiftPressed = (keyData &amp; Keys.Shift) != 0;\n    Keys unmodifiedKey = (keyData &amp; Keys.KeyCode);\n\n    // rest of code goes here\n}\n</code></pre>\n'}, {'answer_id': 2033811, 'author': 'cdiggins', 'author_id': 184528, 'author_profile': 'https://Stackoverflow.com/users/184528', 'pm_score': 3, 'selected': False, 'text': '<p>I upvoted <a href="https://stackoverflow.com/questions/4850/c-and-arrow-keys/5026#5026">Tokabi\'s answer</a>, but for comparing keys there is some additional advice on <a href="https://stackoverflow.com/questions/1369312/c-keys-enumeration-confused-keys-alt-or-keys-rbutton-keys-shiftkey-keys-alt/2033796#2033796">StackOverflow.com here</a>. Here are some functions which I used to help simplify everything.</p>\n\n<pre><code>   public Keys UnmodifiedKey(Keys key)\n    {\n        return key &amp; Keys.KeyCode;\n    }\n\n    public bool KeyPressed(Keys key, Keys test)\n    {\n        return UnmodifiedKey(key) == test;\n    }\n\n    public bool ModifierKeyPressed(Keys key, Keys test)\n    {\n        return (key &amp; test) == test;\n    }\n\n    public bool ControlPressed(Keys key)\n    {\n        return ModifierKeyPressed(key, Keys.Control);\n    }\n\n    public bool AltPressed(Keys key)\n    {\n        return ModifierKeyPressed(key, Keys.Alt);\n    }\n\n    public bool ShiftPressed(Keys key)\n    {\n        return ModifierKeyPressed(key, Keys.Shift);\n    }\n\n    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)\n    {\n        if (KeyPressed(keyData, Keys.Left) &amp;&amp; AltPressed(keyData))\n        {\n            int n = code.Text.IndexOfPrev(\'&lt;\', code.SelectionStart);\n            if (n &lt; 0) return false;\n            if (ShiftPressed(keyData))\n            {\n                code.ExpandSelectionLeftTo(n);\n            }\n            else\n            {\n                code.SelectionStart = n;\n                code.SelectionLength = 0;\n            }\n            return true;\n        }\n        else if (KeyPressed(keyData, Keys.Right) &amp;&amp; AltPressed(keyData))\n        {\n            if (ShiftPressed(keyData))\n            {\n                int n = code.Text.IndexOf(\'&gt;\', code.SelectionEnd() + 1);\n                if (n &lt; 0) return false;\n                code.ExpandSelectionRightTo(n + 1);\n            }\n            else\n            {\n                int n = code.Text.IndexOf(\'&lt;\', code.SelectionStart + 1);\n                if (n &lt; 0) return false;\n                code.SelectionStart = n;\n                code.SelectionLength = 0;\n            }\n            return true;\n        }\n        return base.ProcessCmdKey(ref msg, keyData);\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/4850', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/41/']