/**
         * This updates the state of the device and triggers events
         * to any registered listeners.
         **/
        public void Update()
        {
            try
            {
                XInputState gpState;
                XInputMethods.GetState(deviceID, out gpState);
                XInputGamepad gp = gpState.Gamepad;
               // If previously disconnected, notify of connect
                if (!isConnected && OnConnect != null)
                {
                    isConnected = true;
                    OnConnect(this, new GamepadEventArgs(deviceID, GamepadState.Connected));
                }
 
                if (isConnected)
                {
                    // A Button
                    // Check to see if anything changed
                    if (!(lastAButtonState == gp.IsAButtonDown))
                    {
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.A, gp.IsAButtonDown);

                        if (gp.IsAButtonDown) // If button changed to press state
                        {
                            if (OnAButtonPress != null) OnAButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnAButtonRelease != null) OnAButtonRelease(this, btnEvt);
                        }

                        lastAButtonState = gp.IsAButtonDown;
                    }

                    // B Button
                    // Check to see if anything changed
                    if (!(lastBButtonState == gp.IsBButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.B, gp.IsBButtonDown);

                        if (gp.IsBButtonDown) // If button changed to press state
                        {
                            if (OnBButtonPress != null) OnBButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnBButtonRelease != null) OnBButtonRelease(this, btnEvt);
                        }

                        lastBButtonState = gp.IsBButtonDown;
                    }

                    // X Button
                    // Check to see if anything changed
                    if (!(lastXButtonState == gp.IsXButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.X, gp.IsXButtonDown);

                        if (gp.IsXButtonDown) // If button changed to press state
                        {
                            if (OnXButtonPress != null) OnXButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnXButtonRelease != null) OnXButtonRelease(this, btnEvt);
                        }

                        lastXButtonState = gp.IsXButtonDown;
                    }

                    // Y Button
                    // Check to see if anything changed
                    if (!(lastYButtonState == gp.IsYButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.Y, gp.IsYButtonDown);

                        if (gp.IsYButtonDown) // If button changed to press state
                        {
                            if (OnYButtonPress != null) OnYButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnYButtonRelease != null) OnYButtonRelease(this, btnEvt);
                        }

                        lastYButtonState = gp.IsYButtonDown;
                    }

                    // Back Button
                    // Check to see if anything changed
                    if (!(lastBackButtonState == gp.IsBackButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.Back, gp.IsBackButtonDown);

                        if (gp.IsBackButtonDown) // If button changed to press state
                        {
                            if (OnBackButtonPress != null) OnBackButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnBackButtonRelease != null) OnBackButtonRelease(this, btnEvt);
                        }

                        lastBackButtonState = gp.IsBackButtonDown;
                    }

                    // Start Button
                    // Check to see if anything changed
                    if (!(lastStartButtonState == gp.IsStartButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.Start, gp.IsStartButtonDown);

                        if (gp.IsStartButtonDown) // If button changed to press state
                        {
                            if (OnStartButtonPress != null) OnStartButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnStartButtonRelease != null) OnStartButtonRelease(this, btnEvt);
                        }

                        lastStartButtonState = gp.IsStartButtonDown;
                    }

                    // Left Thumb Button
                    // Check to see if anything changed
                    if (!(lastLeftThumbButtonState == gp.IsLeftThumbButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.LeftThumb, gp.IsLeftThumbButtonDown);

                        if (gp.IsLeftThumbButtonDown) // If button changed to press state
                        {
                            if (OnLeftThumbButtonPress != null) OnLeftThumbButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnLeftThumbButtonRelease != null) OnLeftThumbButtonRelease(this, btnEvt);
                        }

                        lastLeftThumbButtonState = gp.IsLeftThumbButtonDown;
                    }

                    // Right Thumb Button
                    // Check to see if anything changed
                    if (!(lastRightThumbButtonState == gp.IsRightThumbButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.RightThumb, gp.IsRightThumbButtonDown);

                        if (gp.IsRightThumbButtonDown) // If button changed to press state
                        {
                            if (OnRightThumbButtonPress != null) OnRightThumbButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnRightThumbButtonRelease != null) OnRightThumbButtonRelease(this, btnEvt);
                        }

                        lastRightThumbButtonState = gp.IsRightThumbButtonDown;
                    }

                    // Left Shoulder Button
                    // Check to see if anything changed
                    if (!(lastLeftShoulderButtonState == gp.IsLeftShoulderButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.LeftShoulder, gp.IsLeftShoulderButtonDown);

                        if (gp.IsLeftShoulderButtonDown) // If button changed to press state
                        {
                            if (OnLeftShoulderButtonPress != null) OnLeftShoulderButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnLeftShoulderButtonRelease != null) OnLeftShoulderButtonRelease(this, btnEvt);
                        }

                        lastLeftShoulderButtonState = gp.IsLeftShoulderButtonDown;
                    }

                    // Right Shoulder Button
                    // Check to see if anything changed
                    if (!(lastRightShoulderButtonState == gp.IsRightShoulderButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.RightShoulder, gp.IsRightShoulderButtonDown);

                        if (gp.IsRightShoulderButtonDown) // If button changed to press state
                        {
                            if (OnRightShoulderButtonPress != null) OnRightShoulderButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnRightShoulderButtonRelease != null) OnRightShoulderButtonRelease(this, btnEvt);
                        }

                        lastRightShoulderButtonState = gp.IsRightShoulderButtonDown;
                    }

                    // DPad
                    // Check to see if DPad state has changed.
                    GamepadButtons tmpButtons = (GamepadButtons)gp.Buttons;
                    GamepadButtons dpNewState = (GamepadButtons)((int)tmpButtons % 0x10); // Remove other button states
                    if (dpNewState != _lastDPadState) // A change has occured
                    {
                        GamepadDPadEventArgs padEvt = new GamepadDPadEventArgs(deviceID, dpNewState);

                        if (dpNewState == 0) // DPad has been released
                        {
                            if (OnDPadRelease != null) 
                                if(!(_RightThumbMoving || _LeftThumbMoving))
                            {
                                OnDPadRelease(this, padEvt);
                            }
                        }
                        else if (_lastDPadState == 0) // DPad has been pressed
                        {
                            if (OnDPadPress != null)
                            {
                                if (!(_RightThumbMoving || _LeftThumbMoving))
                                {
                                    OnDPadPress(this, padEvt);
                                }
                            }
                        }

                        if (OnDPadChange != null) OnDPadChange(this, padEvt);

                        _lastDPadState = dpNewState;
                    }
                    else if ((_lastDPadState == GamepadButtons.DPadUp) || (_lastDPadState == GamepadButtons.DPadDown)) // If the button is pressed
                    {
                        if (!(_RightThumbMoving || _LeftThumbMoving))
                        {
                            GamepadDPadEventArgs padEvt = new GamepadDPadEventArgs(deviceID, dpNewState);
                            OnDPadPress(this, padEvt);
                        }

                    }

                    // Left Trigger
                    // Check to see if left trigger has changed.
                    if (lastLeftTrigger != gp.LeftTrigger) // A change has occured
                    {
                        GamepadTriggerEventArgs trigEvt = new GamepadTriggerEventArgs(deviceID, GamepadTriggers.Left, gp.LeftTrigger);

                        if (!leftTriggerPressed) // Trigger was pressed
                        {
                            leftTriggerPressed = true;
                            if (OnLeftTriggerPress != null) OnLeftTriggerPress(this, trigEvt);
                        }
                        else if (gp.LeftTrigger == 0) // Trigger was released
                        {
                            if (OnLeftTriggerRelease != null) OnLeftTriggerRelease(this, trigEvt);
                            leftTriggerPressed = false;
                        }

                        if (OnLeftTriggerChange != null) OnLeftTriggerChange(this, trigEvt);

                        lastLeftTrigger = gp.LeftTrigger;
                    }

                    // Right Trigger
                    // Check to see if right trigger has changed.
                    if (lastRightTrigger != gp.RightTrigger) // A change has occured
                    {
                        GamepadTriggerEventArgs trigEvt = new GamepadTriggerEventArgs(deviceID, GamepadTriggers.Right, gp.RightTrigger);

                        if (!rightTriggerPressed) // Trigger was pressed
                        {
                            rightTriggerPressed = true;
                            if (OnRightTriggerPress != null) OnRightTriggerPress(this, trigEvt);
                        }
                        else if (gp.RightTrigger == 0) // Trigger was released
                        {
                            if (OnRightTriggerRelease != null) OnRightTriggerRelease(this, trigEvt);
                            rightTriggerPressed = false;
                        }

                        if (OnRightTriggerChange != null) OnRightTriggerChange(this, trigEvt);

                        lastRightTrigger = gp.RightTrigger;
                    }

                    // Left Thumb Joystick
                    GamepadThumbEventArgs joyEvt = new GamepadThumbEventArgs(deviceID, GamepadThumbs.Left, gp.LeftThumbX, gp.LeftThumbY, gp.IsLeftThumbButtonDown);
                    if (!isThumbPostionInsideRadius(GamepadButtons.LeftThumb, gp.LeftThumbX, gp.LeftThumbY, 8000.0))
                    {
                        if (OnLeftThumbUpdate != null)
                        {
                            if (_RightThumbMoving)
                            {
                                _Move3Axis = true;
                                OnLeftThumbUpdate(this, joyEvt);
                                _LeftThumbMoving = true;
                            }

                            else
                            {
                                _Move3Axis = false;
                                OnLeftThumbUpdate(this, joyEvt);
                                _LeftThumbMoving = true;

                            }
                        
                        }

                    }
                    else
                    {
                        if (IsNoButtonHeldDown(gp))
                        {
                            if (OnThumbBackInsideRadius != null) OnThumbBackInsideRadius(this, joyEvt);
                            _LeftThumbMoving = false;
                        }
                    }

                    // Right Thumb Joystick
                    GamepadThumbEventArgs joyEvt1 = new GamepadThumbEventArgs(deviceID, GamepadThumbs.Right, gp.RightThumbX, gp.RightThumbY, gp.IsRightThumbButtonDown);
                    if (!isThumbPostionInsideRadius(GamepadButtons.RightThumb, gp.RightThumbX, gp.RightThumbY, 8000.0))
                    {
                        if (OnRightThumbUpdate != null)
                        {
                            if (!gp.IsDPadUpButtonDown && !gp.IsDPadDownButtonDown && !_LeftThumbMoving)
                            {
                                _Move3Axis = false;
                                OnRightThumbUpdate(this, joyEvt1);
                                _RightThumbMoving = true;
                            }
                            else
                            {

                                _Move3Axis = true;
                                OnRightThumbUpdate(this, joyEvt1);
                                _RightThumbMoving = true;

                            }
                        }
                    }
                    else
                    {
                        if ( (IsNoButtonHeldDown(gp) && !_LeftThumbMoving) )
                        {
                            if (OnThumbBackInsideRadius != null) OnThumbBackInsideRadius(this, joyEvt1);
                            _RightThumbMoving = false;
                        }
                    }
                    // Update complete
                }
            }
            catch (Win32Exception e)
            {
                if (e.NativeErrorCode == 1167) // This is a disconnect error
                {
                    if (OnDisconnect != null) OnDisconnect(this, new GamepadEventArgs(deviceID, GamepadState.Disconnected));
                    isConnected = false;
                }
                else
                {
                    throw e; // Unknown error, rethrow
                }
            }
        }
Exemple #2
0
        /**
         * This updates the state of the device and triggers events
         * to any registered listeners.
         **/
        public void Update()
        {
            try
            {
                XInputState gpState;
                XInputMethods.GetState(deviceID, out gpState);
                XInputGamepad gp = gpState.Gamepad;
                // If previously disconnected, notify of connect
                if (!isConnected && OnConnect != null)
                {
                    isConnected = true;
                    OnConnect(this, new GamepadEventArgs(deviceID, GamepadState.Connected));
                }

                if (isConnected)
                {
                    // A Button
                    // Check to see if anything changed
                    if (!(lastAButtonState == gp.IsAButtonDown))
                    {
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.A, gp.IsAButtonDown);

                        if (gp.IsAButtonDown) // If button changed to press state
                        {
                            if (OnAButtonPress != null)
                            {
                                OnAButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnAButtonRelease != null)
                            {
                                OnAButtonRelease(this, btnEvt);
                            }
                        }

                        lastAButtonState = gp.IsAButtonDown;
                    }

                    // B Button
                    // Check to see if anything changed
                    if (!(lastBButtonState == gp.IsBButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.B, gp.IsBButtonDown);

                        if (gp.IsBButtonDown) // If button changed to press state
                        {
                            if (OnBButtonPress != null)
                            {
                                OnBButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnBButtonRelease != null)
                            {
                                OnBButtonRelease(this, btnEvt);
                            }
                        }

                        lastBButtonState = gp.IsBButtonDown;
                    }

                    // X Button
                    // Check to see if anything changed
                    if (!(lastXButtonState == gp.IsXButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.X, gp.IsXButtonDown);

                        if (gp.IsXButtonDown) // If button changed to press state
                        {
                            if (OnXButtonPress != null)
                            {
                                OnXButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnXButtonRelease != null)
                            {
                                OnXButtonRelease(this, btnEvt);
                            }
                        }

                        lastXButtonState = gp.IsXButtonDown;
                    }

                    // Y Button
                    // Check to see if anything changed
                    if (!(lastYButtonState == gp.IsYButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.Y, gp.IsYButtonDown);

                        if (gp.IsYButtonDown) // If button changed to press state
                        {
                            if (OnYButtonPress != null)
                            {
                                OnYButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnYButtonRelease != null)
                            {
                                OnYButtonRelease(this, btnEvt);
                            }
                        }

                        lastYButtonState = gp.IsYButtonDown;
                    }

                    // Back Button
                    // Check to see if anything changed
                    if (!(lastBackButtonState == gp.IsBackButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.Back, gp.IsBackButtonDown);

                        if (gp.IsBackButtonDown) // If button changed to press state
                        {
                            if (OnBackButtonPress != null)
                            {
                                OnBackButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnBackButtonRelease != null)
                            {
                                OnBackButtonRelease(this, btnEvt);
                            }
                        }

                        lastBackButtonState = gp.IsBackButtonDown;
                    }

                    // Start Button
                    // Check to see if anything changed
                    if (!(lastStartButtonState == gp.IsStartButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.Start, gp.IsStartButtonDown);

                        if (gp.IsStartButtonDown) // If button changed to press state
                        {
                            if (OnStartButtonPress != null)
                            {
                                OnStartButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnStartButtonRelease != null)
                            {
                                OnStartButtonRelease(this, btnEvt);
                            }
                        }

                        lastStartButtonState = gp.IsStartButtonDown;
                    }

                    // Left Thumb Button
                    // Check to see if anything changed
                    if (!(lastLeftThumbButtonState == gp.IsLeftThumbButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.LeftThumb, gp.IsLeftThumbButtonDown);

                        if (gp.IsLeftThumbButtonDown) // If button changed to press state
                        {
                            if (OnLeftThumbButtonPress != null)
                            {
                                OnLeftThumbButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnLeftThumbButtonRelease != null)
                            {
                                OnLeftThumbButtonRelease(this, btnEvt);
                            }
                        }

                        lastLeftThumbButtonState = gp.IsLeftThumbButtonDown;
                    }

                    // Right Thumb Button
                    // Check to see if anything changed
                    if (!(lastRightThumbButtonState == gp.IsRightThumbButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.RightThumb, gp.IsRightThumbButtonDown);

                        if (gp.IsRightThumbButtonDown) // If button changed to press state
                        {
                            if (OnRightThumbButtonPress != null)
                            {
                                OnRightThumbButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnRightThumbButtonRelease != null)
                            {
                                OnRightThumbButtonRelease(this, btnEvt);
                            }
                        }

                        lastRightThumbButtonState = gp.IsRightThumbButtonDown;
                    }

                    // Left Shoulder Button
                    // Check to see if anything changed
                    if (!(lastLeftShoulderButtonState == gp.IsLeftShoulderButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.LeftShoulder, gp.IsLeftShoulderButtonDown);

                        if (gp.IsLeftShoulderButtonDown) // If button changed to press state
                        {
                            if (OnLeftShoulderButtonPress != null)
                            {
                                OnLeftShoulderButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnLeftShoulderButtonRelease != null)
                            {
                                OnLeftShoulderButtonRelease(this, btnEvt);
                            }
                        }

                        lastLeftShoulderButtonState = gp.IsLeftShoulderButtonDown;
                    }

                    // Right Shoulder Button
                    // Check to see if anything changed
                    if (!(lastRightShoulderButtonState == gp.IsRightShoulderButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.RightShoulder, gp.IsRightShoulderButtonDown);

                        if (gp.IsRightShoulderButtonDown) // If button changed to press state
                        {
                            if (OnRightShoulderButtonPress != null)
                            {
                                OnRightShoulderButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnRightShoulderButtonRelease != null)
                            {
                                OnRightShoulderButtonRelease(this, btnEvt);
                            }
                        }

                        lastRightShoulderButtonState = gp.IsRightShoulderButtonDown;
                    }

                    // DPad
                    // Check to see if DPad state has changed.
                    GamepadButtons tmpButtons = (GamepadButtons)gp.Buttons;
                    GamepadButtons dpNewState = (GamepadButtons)((int)tmpButtons % 0x10); // Remove other button states
                    if (dpNewState != _lastDPadState)                                     // A change has occured
                    {
                        GamepadDPadEventArgs padEvt = new GamepadDPadEventArgs(deviceID, dpNewState);

                        if (dpNewState == 0) // DPad has been released
                        {
                            if (OnDPadRelease != null)
                            {
                                if (!(_RightThumbMoving || _LeftThumbMoving))
                                {
                                    OnDPadRelease(this, padEvt);
                                }
                            }
                        }
                        else if (_lastDPadState == 0) // DPad has been pressed
                        {
                            if (OnDPadPress != null)
                            {
                                if (!(_RightThumbMoving || _LeftThumbMoving))
                                {
                                    OnDPadPress(this, padEvt);
                                }
                            }
                        }

                        if (OnDPadChange != null)
                        {
                            OnDPadChange(this, padEvt);
                        }

                        _lastDPadState = dpNewState;
                    }
                    else if ((_lastDPadState == GamepadButtons.DPadUp) || (_lastDPadState == GamepadButtons.DPadDown)) // If the button is pressed
                    {
                        if (!(_RightThumbMoving || _LeftThumbMoving))
                        {
                            GamepadDPadEventArgs padEvt = new GamepadDPadEventArgs(deviceID, dpNewState);
                            OnDPadPress(this, padEvt);
                        }
                    }

                    // Left Trigger
                    // Check to see if left trigger has changed.
                    if (lastLeftTrigger != gp.LeftTrigger) // A change has occured
                    {
                        GamepadTriggerEventArgs trigEvt = new GamepadTriggerEventArgs(deviceID, GamepadTriggers.Left, gp.LeftTrigger);

                        if (!leftTriggerPressed) // Trigger was pressed
                        {
                            leftTriggerPressed = true;
                            if (OnLeftTriggerPress != null)
                            {
                                OnLeftTriggerPress(this, trigEvt);
                            }
                        }
                        else if (gp.LeftTrigger == 0) // Trigger was released
                        {
                            if (OnLeftTriggerRelease != null)
                            {
                                OnLeftTriggerRelease(this, trigEvt);
                            }
                            leftTriggerPressed = false;
                        }

                        if (OnLeftTriggerChange != null)
                        {
                            OnLeftTriggerChange(this, trigEvt);
                        }

                        lastLeftTrigger = gp.LeftTrigger;
                    }

                    // Right Trigger
                    // Check to see if right trigger has changed.
                    if (lastRightTrigger != gp.RightTrigger) // A change has occured
                    {
                        GamepadTriggerEventArgs trigEvt = new GamepadTriggerEventArgs(deviceID, GamepadTriggers.Right, gp.RightTrigger);

                        if (!rightTriggerPressed) // Trigger was pressed
                        {
                            rightTriggerPressed = true;
                            if (OnRightTriggerPress != null)
                            {
                                OnRightTriggerPress(this, trigEvt);
                            }
                        }
                        else if (gp.RightTrigger == 0) // Trigger was released
                        {
                            if (OnRightTriggerRelease != null)
                            {
                                OnRightTriggerRelease(this, trigEvt);
                            }
                            rightTriggerPressed = false;
                        }

                        if (OnRightTriggerChange != null)
                        {
                            OnRightTriggerChange(this, trigEvt);
                        }

                        lastRightTrigger = gp.RightTrigger;
                    }

                    // Left Thumb Joystick
                    GamepadThumbEventArgs joyEvt = new GamepadThumbEventArgs(deviceID, GamepadThumbs.Left, gp.LeftThumbX, gp.LeftThumbY, gp.IsLeftThumbButtonDown);
                    if (!isThumbPostionInsideRadius(GamepadButtons.LeftThumb, gp.LeftThumbX, gp.LeftThumbY, 8000.0))
                    {
                        if (OnLeftThumbUpdate != null)
                        {
                            if (_RightThumbMoving)
                            {
                                _Move3Axis = true;
                                OnLeftThumbUpdate(this, joyEvt);
                                _LeftThumbMoving = true;
                            }

                            else
                            {
                                _Move3Axis = false;
                                OnLeftThumbUpdate(this, joyEvt);
                                _LeftThumbMoving = true;
                            }
                        }
                    }
                    else
                    {
                        if (IsNoButtonHeldDown(gp))
                        {
                            if (OnThumbBackInsideRadius != null)
                            {
                                OnThumbBackInsideRadius(this, joyEvt);
                            }
                            _LeftThumbMoving = false;
                        }
                    }

                    // Right Thumb Joystick
                    GamepadThumbEventArgs joyEvt1 = new GamepadThumbEventArgs(deviceID, GamepadThumbs.Right, gp.RightThumbX, gp.RightThumbY, gp.IsRightThumbButtonDown);
                    if (!isThumbPostionInsideRadius(GamepadButtons.RightThumb, gp.RightThumbX, gp.RightThumbY, 8000.0))
                    {
                        if (OnRightThumbUpdate != null)
                        {
                            if (!gp.IsDPadUpButtonDown && !gp.IsDPadDownButtonDown && !_LeftThumbMoving)
                            {
                                _Move3Axis = false;
                                OnRightThumbUpdate(this, joyEvt1);
                                _RightThumbMoving = true;
                            }
                            else
                            {
                                _Move3Axis = true;
                                OnRightThumbUpdate(this, joyEvt1);
                                _RightThumbMoving = true;
                            }
                        }
                    }
                    else
                    {
                        if ((IsNoButtonHeldDown(gp) && !_LeftThumbMoving))
                        {
                            if (OnThumbBackInsideRadius != null)
                            {
                                OnThumbBackInsideRadius(this, joyEvt1);
                            }
                            _RightThumbMoving = false;
                        }
                    }
                    // Update complete
                }
            }
            catch (Win32Exception e)
            {
                if (e.NativeErrorCode == 1167) // This is a disconnect error
                {
                    if (OnDisconnect != null)
                    {
                        OnDisconnect(this, new GamepadEventArgs(deviceID, GamepadState.Disconnected));
                    }
                    isConnected = false;
                }
                else
                {
                    throw e; // Unknown error, rethrow
                }
            }
        }
        private void OnDPadPress(object sender, GamepadDPadEventArgs evt)
        {
            Text = "DPad pressed";
            bool DownIsDown = (evt.Buttons & GamepadButtons.DPadDown) != 0;
            bool LeftIsDown = (evt.Buttons & GamepadButtons.DPadLeft) != 0;
            bool RightIsDown = (evt.Buttons & GamepadButtons.DPadRight) != 0;
            bool UpIsDown = (evt.Buttons & GamepadButtons.DPadUp) != 0;
            double x, y, z;
            double deltaz = 0.05;
            if (UpIsDown || DownIsDown)
            {

                if (UpIsDown)
                {
                    myStage.GetPos(out x, out y, out z);
                    myStage.MoveTo(x, y, z + deltaz, false);
                }
                if (DownIsDown)
                {
                    myStage.GetPos(out x, out y, out z);
                    myStage.MoveTo(x, y, z - deltaz, false);
                }
            }

            Thread.Sleep(50);


        }
 private void OnDPadRelease(object sender, GamepadDPadEventArgs evt)
 {
     Text = "DPad released";
     myStage.Stop();
 }
        private void OnDPadChange(object sender, GamepadDPadEventArgs evt)
        {
            // Reset
            //DPadDownHighlight.Visible = false;
            //DPadDownLeftHighlight.Visible = false;
            //DPadDownRightHighlight.Visible = false;
            //DPadLeftHighlight.Visible = false;
            //DPadLeftHighlight.Visible = false;
            //DPadRightHighlight.Visible = false;
            //DPadUpHighlight.Visible = false;
            //DPadUpLeftHighlight.Visible = false;
            //DPadUpRightHighlight.Visible = false;

            bool DownIsDown = (evt.Buttons & GamepadButtons.DPadDown) != 0;
            bool LeftIsDown = (evt.Buttons & GamepadButtons.DPadLeft) != 0;
            bool RightIsDown = (evt.Buttons & GamepadButtons.DPadRight) != 0;
            bool UpIsDown = (evt.Buttons & GamepadButtons.DPadUp) != 0;

            //if (DownIsDown) DPadDownHighlight.Visible = true;

            //if (LeftIsDown) DPadLeftHighlight.Visible = true;

            //if (RightIsDown) DPadRightHighlight.Visible = true;

            //if (UpIsDown) DPadUpHighlight.Visible = true;

            //if (DownIsDown && LeftIsDown)
            //{
            //    DPadDownHighlight.Visible = false;
            //    DPadLeftHighlight.Visible = false;
            //    DPadDownLeftHighlight.Visible = true;
            //}

            //if (DownIsDown && RightIsDown)
            //{
            //    DPadDownHighlight.Visible = false;
            //    DPadRightHighlight.Visible = false;
            //    DPadDownRightHighlight.Visible = true;
            //}

            //if (UpIsDown && LeftIsDown)
            //{
            //    DPadUpHighlight.Visible = false;
            //    DPadLeftHighlight.Visible = false;
            //    DPadUpLeftHighlight.Visible = true;
            //}

            //if (UpIsDown && RightIsDown)
            //{
            //    DPadUpHighlight.Visible = false;
            //    DPadRightHighlight.Visible = false;
            //    DPadUpRightHighlight.Visible = true;
            //}
        }