0

How can I access the coordinates of the two Xbox One thumbsticks in a UWP application? Also, how can I detect when the X, Y, A and B buttons are pressed?

Edit: I have been getting an exception of type "System.InvalidOperationException" in System.Linq.dll when I use Gamepad.Gamepads.First() in the MainPage method in MainPage.xaml.cs

Dhruv
  • 117
  • 9

2 Answers2

2

You should look at gaming technologies for UWP and in particular Windows.Gaming.Input API.

In short: after you get access to your gamepad:

 var controller = Gamepad.Gamepads.First();

You start reading GamepadReadings from it:

 var reading = controller.GetCurrentReading();

Each reading contains data on thumbsticks, triggers and buttons.

 var leftThumbstickX = reading.LeftThumbstickX;
 var aButton = (reading.Buttons & GamepadButtons.A);
Konstantin
  • 884
  • 6
  • 12
  • Gamepad.Gamepads.Count returns 0 for me even when I have my Xbox One controller switched on while the application is running on Xbox. Hence I have an exception at the following statement: var controller = Gamepad.Gamepads.First(); Any idea how to resolve this issue? – Dhruv Jun 05 '16 at 23:53
  • I have been getting an exception of type "System.InvalidOperationException" in System.Linq.dll when I use Gamepad.Gamepads.First() in the MainPage method in MainPage.xaml.cs – Dhruv Jun 06 '16 at 15:37
  • It means, your controller is not visible for application. If you connect it to PC and your UWP app in PC - does it work? – Konstantin Jun 07 '16 at 06:32
1

Not sure about the thumbsticks but you can detect the gamepad buttons as any other key press event.

Just hook into the KeyUp event of you page or to the global Window.Current.CoreWindow.KeyUp event and check for the gamepad buttons like VirtualKey.GamepadB.

This works when the app runs on XBox One and also when you connect the XBox One controller to your PC using a USB cable.

Igor Kulman
  • 16,211
  • 10
  • 57
  • 118
  • The D-pad and thumbsticks fire keyup events as well. You can see the actual keycodes for each here: https://github.com/Microsoft/TVHelpers/blob/master/tvjs/src/DirectionalNavigation/directionalnavigation-1.0.0.0.js#L701 – Bart Jun 07 '16 at 22:47
  • Thanks, this is much easier to use than polling the Gamepad readings. – Martin Suchan Aug 09 '16 at 20:10