private void beginCapture(TextBox keyBox)
        {
            KeyBindControlState state = (KeyBindControlState)keyBox.Tag;

            keyBox.BackColor = Color.Yellow;
            state.capturing  = true;
            escapeNoClose    = true;
        }
        private void keyBoxKeyDown(object sender, KeyEventArgs e)
        {
            TextBox             box   = (TextBox)sender;
            KeyBindControlState state = (KeyBindControlState)box.Tag;

            // When focused, space starts capturing
            if (!state.capturing && e.KeyCode == Keys.Space)
            {
                beginCapture(box);
                return;
            }

            // When capturing, escape ends capturing
            if (state.capturing && e.KeyCode == Keys.Escape)
            {
                endCapture(box, Keys.None);
                return;
            }

            // Otherwise, normal capture end
            if (state.capturing)
            {
                // First, do some checking for keys that the .NET events don't handle well correctly

                if (keyDown(Keys.LControlKey))
                {
                    endCapture(box, Keys.LControlKey);
                }
                else if (keyDown(Keys.RControlKey))
                {
                    endCapture(box, Keys.RControlKey);
                }
                else if (keyDown(Keys.LShiftKey))
                {
                    endCapture(box, Keys.LShiftKey);
                }
                else if (keyDown(Keys.RShiftKey))
                {
                    endCapture(box, Keys.RShiftKey);
                }
                else if (keyDown(Keys.LMenu))
                {
                    endCapture(box, Keys.LMenu);
                }
                else if (keyDown(Keys.RMenu))
                {
                    endCapture(box, Keys.RMenu);
                }
                else
                {
                    endCapture(box, e.KeyCode);
                }
            }
        }
        private void endCapture(TextBox keyBox, Keys key)
        {
            KeyBindControlState state = (KeyBindControlState)keyBox.Tag;

            if (!state.capturing)
            {
                return;
            }

            state.capturing = false;
            escapeNoClose   = false;
            if (key != Keys.None)
            {
                state.key = key;
            }
            keyBox.BackColor = SystemColors.Window;
            KeyBindControls.updateBox(keyBox);
            // Update box contents
        }