Xbox One Controller input to UWP App

2020-04-30 03:32发布

问题:

I have been trying to make an Xbox One controller interact with a UWP application and have looked into the Gamepad class (based on the suggestions mentioned in the comments - Controller support for Xbox one in Windows UWP). I have 2 issues:

1) Gamepad.Gamepads.Count returns 0 for me even when I have my Xbox One controller switched on while the application is running on Xbox.

2) I do not know, how exactly can I assess when the A, B, X and Y buttons are pressed and also access the coordinates of the Left and Right Thumbsticks.

Any guidance about the 2 points mentioned above, would be very helpful for me. Thanks!

回答1:

One way to handle input, via the controller, is by just using keypress events.

document.addEventListener('keypress', function(e){
    switch (e.keyCode) {
        case 211:  // GamepadLeftThumbstickUp
        case 203:  // GamepadDPadUp
            break;

        case 212:  // GamepadLeftThumbstickDown
        case 204:  // GamepadDPadDown
            break;

        case 214:  // GamepadLeftThumbstickLeft
        case 205:  // GamepadDPadLeft
            break;

        case 213:  // GamepadLeftThumbstickRight
        case 206:  // GamepadDPadRight
            break;

        case 195:  // A Button
            break;

        case 196: // B button
            break;

        case 197: // X Button
            break;

        case 198: // Y Button
            break;

        case 208: // View button
            break;

        case 207: // Menu button
            break;

        case 200: // Left Bumper
            break;

        case 199: // Right Bumper
            break;

        case 201: // Left Trigger
            break;

        case 202: // Right Trigger
            break;

    }
});