示例#1
0
        /// <summary>Called when a key on the keyboard has been pressed down</summary>
        /// <param name="keyCode">Code of the key that was pressed</param>
        public void InjectKeyPress(Keys keyCode)
        {
            var repetition = _heldKeys.Get((int)keyCode);

            // If a control is activated, it will receive any input notifications
            if (_activatedControl != null)
            {
                _activatedControl.ProcessKeyPress(keyCode, repetition);
                if (!repetition)
                {
                    ++_heldKeyCount;
                    _heldKeys.Set((int)keyCode, true);
                }
                return;
            }

            // No control is activated, try the focused control before searching
            // the entire tree for a responder.
            var focusedControl = _focusedControl.Target;

            if (focusedControl != null)
            {
                if (focusedControl.ProcessKeyPress(keyCode, false))
                {
                    _activatedControl = focusedControl;
                    if (!repetition)
                    {
                        ++_heldKeyCount;
                        _heldKeys.Set((int)keyCode, true);
                    }
                    return;
                }
            }

            // Focused control didn't process the notification, now let the desktop
            // control traverse the entire control tree is earch for a handler.
            if (_desktopControl.ProcessKeyPress(keyCode, false))
            {
                _activatedControl = _desktopControl;
                if (!repetition)
                {
                    ++_heldKeyCount;
                    _heldKeys.Set((int)keyCode, true);
                }
            }
            else
            {
                switch (keyCode)
                {
                case Keys.Up:
                {
                    InjectCommand(Command.Up);
                    break;
                }

                case Keys.Down:
                {
                    InjectCommand(Command.Down);
                    break;
                }

                case Keys.Left:
                {
                    InjectCommand(Command.Left);
                    break;
                }

                case Keys.Right:
                {
                    InjectCommand(Command.Right);
                    break;
                }

                case Keys.Enter:
                {
                    InjectCommand(Command.Accept);
                    break;
                }

                case Keys.Escape:
                {
                    InjectCommand(Command.Cancel);
                    break;
                }
                }
            }
        }