Reputation: 117
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
Upvotes: 0
Views: 675
Reputation: 16361
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.
Upvotes: 1
Reputation: 884
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);
Upvotes: 2