One of the reasons I bought an Xbox instead of a PS4 is that Microsoft made a promise that every console could be unlocked for developers. Since I’m fairly familiar with C#, Visual Studio and the Windows ecosystem in general, the choice made sense.
Let’s see what you can get out of the Xbox One controller using the standard Universal App API’s. Seems it’s fairly simple and uses a minimum amount of code.
Note: unfortunately you cannot use the Xbox one controller directly on a Raspberry PI 2/ 3 with Windows 10 IOT since there is, at the moment of writing, no driver for it…
Create a background task that polls the controller and translates everything into an Event:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
public async Task PollGamepad() { while (true) { if (_running) { foreach (Gamepad controller in _controllers) { if (controller.GetCurrentReading().Buttons == GamepadButtons.A) { OnXBoxGamepadButtonPressA(controller, controller.GetCurrentReading().Buttons); } if (controller.GetCurrentReading().Buttons == GamepadButtons.B) { OnXBoxGamepadButtonPressB(controller, controller.GetCurrentReading().Buttons); } if (controller.GetCurrentReading().Buttons == GamepadButtons.X) { OnXBoxGamepadButtonPressX(controller, controller.GetCurrentReading().Buttons); } if (controller.GetCurrentReading().Buttons == GamepadButtons.Y) { OnXBoxGamepadButtonPressY(controller, controller.GetCurrentReading().Buttons); } if (controller.GetCurrentReading().Buttons == GamepadButtons.DPadLeft) { OnXBoxGamepadButtonPressLeft(controller, controller.GetCurrentReading().Buttons); } if (controller.GetCurrentReading().Buttons == GamepadButtons.DPadUp) { OnXBoxGamepadButtonPressUp(controller, controller.GetCurrentReading().Buttons); } if (controller.GetCurrentReading().Buttons == GamepadButtons.DPadRight) { OnXBoxGamepadButtonPressRight(controller, controller.GetCurrentReading().Buttons); } if (controller.GetCurrentReading().Buttons == GamepadButtons.DPadDown) { OnXBoxGamepadButtonPressDown(controller, controller.GetCurrentReading().Buttons); } if (controller.GetCurrentReading().Buttons == GamepadButtons.Menu) { OnXBoxGamepadButtonMenu(controller, controller.GetCurrentReading().Buttons); } if (controller.GetCurrentReading().Buttons == GamepadButtons.View) { OnXBoxGamepadButtonView(controller, controller.GetCurrentReading().Buttons); } if (controller.GetCurrentReading().LeftThumbstickX >= 0.1 || controller.GetCurrentReading().LeftThumbstickX <= -0.1 || controller.GetCurrentReading().LeftThumbstickY >= 0.1 || controller.GetCurrentReading().LeftThumbstickY <= -0.1) { OnXBoxGamepadLeftStickMove(controller, new List<double>() { controller.GetCurrentReading().LeftThumbstickX, controller.GetCurrentReading().LeftThumbstickY }); } if (controller.GetCurrentReading().RightThumbstickX >= 0.1 || controller.GetCurrentReading().RightThumbstickX <= -0.1 || controller.GetCurrentReading().RightThumbstickY >= 0.1 || controller.GetCurrentReading().RightThumbstickY <= -0.1) { OnXBoxGamepadRightStickMove(controller, new List<double>() { controller.GetCurrentReading().RightThumbstickX, controller.GetCurrentReading().RightThumbstickY }); } } } await Task.Delay(50); } } |
Registering events to consume them later:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
private void btnStart_Click(object sender, RoutedEventArgs e) { gamepad = new BL.XBoxGamepad(); gamepad.OnXBoxGamepadButtonPressA += Gamepad_OnXBoxGamepadButtonPressA; gamepad.OnXBoxGamepadButtonPressB += Gamepad_OnXBoxGamepadButtonPressB; gamepad.OnXBoxGamepadButtonPressX += Gamepad_OnXBoxGamepadButtonPressX; gamepad.OnXBoxGamepadButtonPressY += Gamepad_OnXBoxGamepadButtonPressY; gamepad.OnXBoxGamepadLeftStickMove += Gamepad_OnXBoxGamepadLeftStickMove; gamepad.OnXBoxGamepadRightStickMove += Gamepad_OnXBoxGamepadRightStickMove; gamepad.OnXBoxGamepadButtonMenu += Gamepad_OnXBoxGamepadButtonMenu; gamepad.OnXBoxGamepadButtonView += Gamepad_OnXBoxGamepadButtonView; } |
Consume the event and show something in the GUI:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
private void Gamepad_OnXBoxGamepadButtonPressA(object sender, GamepadButtons e) { UpdateOutput(e.ToString()); } public async void UpdateOutput(string message) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lblOutput.Text = String.Format("{0}{1}{2}", message, Environment.NewLine, lblOutput.Text); } ); } |
All of this is sort of like a proof of concept. It’s up to you to extend this now.
If you want to, you can check out the GitHub repository here.