/// <summary> /// Observes the gamepad for status changes. /// </summary> private void ObserveGamepad() { uint lastPacketNumber = 0; XInput.State state = new XInput.State(); XInput.KeyStroke keystroke = new XInput.KeyStroke(); while (!this.StopObserving) { XInput.Error result = XInput.GetState((XInput.UserIndex) this.UserIndex, out state); if (result == XInput.Error.Success && state.packetNumber > lastPacketNumber) { lastPacketNumber = state.packetNumber; this.SetStateFromXInputState(state); this.RaiseStateChanged(); } result = XInput.GetKeystroke((XInput.UserIndex) this.UserIndex, out keystroke); if (result == XInput.Error.Success) { if (keystroke.flags == XInput.KeyStrokeFlags.KeyUp) { this.RaiseKeyUp((KeyEventArgs.KeyCode)keystroke.virtualKey); } if (keystroke.flags == XInput.KeyStrokeFlags.KeyDown) { this.RaiseKeyDown((KeyEventArgs.KeyCode)keystroke.virtualKey); } } Thread.Sleep(2); } }