private RawButtonInputReport ExtractRawButtonInputReport(NotifyInputEventArgs e, RoutedEvent Event)
        {
            RawButtonInputReport buttonInputReport = (RawButtonInputReport)null;
            InputReportEventArgs input;

            if ((input = e.StagingItem.Input as InputReportEventArgs) != null && input.Report is RawButtonInputReport && input.RoutedEvent == Event)
            {
                buttonInputReport = (RawButtonInputReport)input.Report;
            }
            return(buttonInputReport);
        }
        private RawButtonActions GetNonRedundantActions(NotifyInputEventArgs e)
        {
            RawButtonActions actions;

            // The CLR throws a null-ref exception if it tries to unbox a
            // null.  So we have to special case that.
            var o = e.StagingItem.GetData(this._tagNonRedundantActions);

            if (o != null)
            {
                actions = (RawButtonActions)o;
            }
            else
            {
                actions = new RawButtonActions();
            }

            return(actions);
        }
        private void PreNotifyInput(object sender, NotifyInputEventArgs e)
        {
            var buttonInput = ExtractRawButtonInputReport(e, InputManager.PreviewInputReportEvent);

            if (buttonInput != null)
            {
                CheckForDisconnectedFocus();

                /*
                 *
                 * REFACTOR --
                 *
                 * the keyboard device is only active per app domain basis -- so like if your app domain doesn't have
                 * focus your keyboard device is not going to give you the real state of the keyboard.
                 *
                 * When it gets focus, it needs to know about this somehow.   We could use this keyboard action
                 * type stuff to do so.  Though this design really seem to be influenced a lot from living in
                 * the windows world.
                 *
                 * Essentially the input stack is being used like a message pump to say, hey dude you can
                 * use the keyboard now -- it's not real input, it's more or less a message.
                 *
                 * It could be interesting for elements to know about this -- since I think
                 * they will probalby still have focus (or do they get a Got and Lost Focus when the keyboard activates -- I don't think so,
                 * we need to know what we were focused on when the window gets focus again.
                 *
                 * So maybe elements want to stop some animation or something when input focus moves away from the activesource, and
                 * start them again later.  Could be interesting.
                 */

                if ((buttonInput.Actions & RawButtonActions.Activate) == RawButtonActions.Activate)
                {
                    //System.Console.WriteLine("Initializing the button state.");

#if TRACK_BUTTON_STATE
                    // Clear out our key state storage.
                    for (var i = 0; i < this._buttonState.Length; i++)
                    {
                        this._buttonState[i] = 0;
                    }
#endif
                    // we are now active.
                    // we should track which source is active so we don't confuse deactivations.
                    this._isActive = true;
                }

                // Generally, we need to check against redundant actions.
                // We never prevet the raw event from going through, but we
                // will only generate the high-level events for non-redundant
                // actions.  We store the set of non-redundant actions in
                // the dictionary of this event.

                // If the input is reporting a button down, the action is never
                // considered redundant.
                if ((buttonInput.Actions & RawButtonActions.ButtonDown) == RawButtonActions.ButtonDown)
                {
                    var actions = GetNonRedundantActions(e);
                    actions |= RawButtonActions.ButtonDown;
                    e.StagingItem.SetData(this._tagNonRedundantActions, actions);

                    // Pass along the button that was pressed, and update our state.
                    e.StagingItem.SetData(this._tagButton, buttonInput.Button);

#if TRACK_BUTTON_STATE
                    var buttonState = GetButtonState(buttonInput.Button);

                    if ((buttonState & ButtonState.Down) == ButtonState.Down)
                    {
                        buttonState = ButtonState.Down | ButtonState.Held;
                    }
                    else
                    {
                        buttonState |= ButtonState.Down;
                    }

                    SetButtonState(buttonInput.Button, buttonState);
#endif

                    // Tell the InputManager that the MostRecentDevice is us.
                    if (this._inputManager != null && this._inputManager.MostRecentInputDevice != this)
                    {
                        this._inputManager.MostRecentInputDevice = (InputDevice)this;
                    }
                }

                // need to detect redundant ups.
                if ((buttonInput.Actions & RawButtonActions.ButtonUp) == RawButtonActions.ButtonUp)
                {
                    var actions = GetNonRedundantActions(e);
                    actions |= RawButtonActions.ButtonUp;
                    e.StagingItem.SetData(this._tagNonRedundantActions, actions);

                    // Pass along the button that was pressed, and update our state.
                    e.StagingItem.SetData(this._tagButton, buttonInput.Button);

#if TRACK_BUTTON_STATE
                    var buttonState = GetButtonState(buttonInput.Button);

                    if ((buttonState & ButtonState.Down) == ButtonState.Down)
                    {
                        buttonState &= (~ButtonState.Down) & (ButtonState.Down | ButtonState.Held);
                    }
                    else
                    {
                        buttonState |= ButtonState.Held;
                    }

                    SetButtonState(buttonInput.Button, buttonState);
#endif

                    // Tell the InputManager that the MostRecentDevice is us.
                    if (this._inputManager != null && this._inputManager.MostRecentInputDevice != this)
                    {
                        this._inputManager.MostRecentInputDevice = (InputDevice)this;
                    }
                }
            }

            // On ButtonDown, we might need to set the Repeat flag

            if (e.StagingItem.Input.RoutedEvent == Buttons.PreviewButtonDownEvent)
            {
                CheckForDisconnectedFocus();

                var args = (ButtonEventArgs)e.StagingItem.Input;

                // Is this the same as the previous button?
                if (this._previousButton == args.Button)
                {
                    // Yes, this is a repeat (we got the buttondown for it twice, with no ButtonUp in between)
                    // what about chording?
                    args._isRepeat = true;
                }

                // Otherwise, keep this button to check against next time.
                else
                {
                    this._previousButton = args.Button;
                    args._isRepeat       = false;
                }
            }

            // On ButtonUp, we clear Repeat flag
            else if (e.StagingItem.Input.RoutedEvent == Buttons.PreviewButtonUpEvent)
            {
                CheckForDisconnectedFocus();

                var args = (ButtonEventArgs)e.StagingItem.Input;
                args._isRepeat = false;

                // Clear _previousButton, so that down/up/down/up doesn't look like a repeat
                this._previousButton = HardwareButton.None;
            }
        }
        private RawButtonActions GetNonRedundantActions(NotifyInputEventArgs e)
        {
            object data = e.StagingItem.GetData(this._tagNonRedundantActions);

            return(data == null ? (RawButtonActions)0 : (RawButtonActions)data);
        }
        private void PreNotifyInput(object sender, NotifyInputEventArgs e)
        {
            RawButtonInputReport buttonInputReport = this.ExtractRawButtonInputReport(e, InputManager.PreviewInputReportEvent);

            if (buttonInputReport != null)
            {
                this.CheckForDisconnectedFocus();
                if ((buttonInputReport.Actions & RawButtonActions.Activate) == RawButtonActions.Activate)
                {
                    for (int index = 0; index < this._buttonState.Length; ++index)
                    {
                        this._buttonState[index] = (byte)0;
                    }
                    this._isActive = true;
                }
                if ((buttonInputReport.Actions & RawButtonActions.ButtonDown) == RawButtonActions.ButtonDown)
                {
                    RawButtonActions rawButtonActions = this.GetNonRedundantActions(e) | RawButtonActions.ButtonDown;
                    e.StagingItem.SetData(this._tagNonRedundantActions, (object)rawButtonActions);
                    e.StagingItem.SetData(this._tagButton, (object)buttonInputReport.Button);
                    ButtonState buttonState = this.GetButtonState(buttonInputReport.Button);
                    ButtonState state       = (buttonState & ButtonState.Down) != ButtonState.Down ? buttonState | ButtonState.Down : ButtonState.Down | ButtonState.Held;
                    this.SetButtonState(buttonInputReport.Button, state);
                    if (this._inputManager != null && this._inputManager.MostRecentInputDevice != this)
                    {
                        this._inputManager.MostRecentInputDevice = (InputDevice)this;
                    }
                }
                if ((buttonInputReport.Actions & RawButtonActions.ButtonUp) == RawButtonActions.ButtonUp)
                {
                    RawButtonActions rawButtonActions = this.GetNonRedundantActions(e) | RawButtonActions.ButtonUp;
                    e.StagingItem.SetData(this._tagNonRedundantActions, (object)rawButtonActions);
                    e.StagingItem.SetData(this._tagButton, (object)buttonInputReport.Button);
                    ButtonState buttonState = this.GetButtonState(buttonInputReport.Button);
                    ButtonState state       = (buttonState & ButtonState.Down) != ButtonState.Down ? buttonState | ButtonState.Held : buttonState & ButtonState.Held;
                    this.SetButtonState(buttonInputReport.Button, state);
                    if (this._inputManager != null && this._inputManager.MostRecentInputDevice != this)
                    {
                        this._inputManager.MostRecentInputDevice = (InputDevice)this;
                    }
                }
            }
            if (e.StagingItem.Input.RoutedEvent == Buttons.PreviewButtonDownEvent)
            {
                this.CheckForDisconnectedFocus();
                ButtonEventArgs input = (ButtonEventArgs)e.StagingItem.Input;
                if (this._previousButton == input.Button)
                {
                    input._isRepeat = true;
                }
                else
                {
                    this._previousButton = input.Button;
                    input._isRepeat      = false;
                }
            }
            else
            {
                if (e.StagingItem.Input.RoutedEvent != Buttons.PreviewButtonUpEvent)
                {
                    return;
                }
                this.CheckForDisconnectedFocus();
                ((ButtonEventArgs)e.StagingItem.Input)._isRepeat = false;
                this._previousButton = HardwareButton.None;
            }
        }