2

I've developed a UWP app that could potentially run on XBOX.

I want to detect whether the buttons on the gamepad controller have been pressed (either A B X or Y).

I think I need to be using the click event? If it is on the click event, what do I need to check in the click event?

Looking at this post which determines whether a trigger has been pressed..

Controller support for Xbox one in Windows UWP

/*
 * Note: I have not tested this code.
 * If it is incorrect, please do edit with the fix.
 */
using Windows.Gaming.Input;

// bla bla bla boring stuff...

// Get the first controller
var controller = Gamepad.Gamepads.First();

// Get the current state
var reading = controller.GetCurrentReading();

// Check to see if the left trigger was depressed about half way down
if (reading.LeftTrigger == 0.5;)
{
    // Do whatever
}

I presume there is an equivalent way of checking whether one of the ABXY buttons have been pressed?. I shall check next time I get chance.

On another note, this blog post looks really useful for people starting out with developing UWP for Xbox One http://grogansoft.com/blog/?p=1278

Update : Looks like I can call GetCurrentReading() to get a GamepadReading structure. and from that get the state of the GamepadButtons.

Community
  • 1
  • 1
Lee Englestone
  • 4,545
  • 13
  • 51
  • 85

1 Answers1

4

The KeyDown event from CoreWindow or any other UWP control will be fired even when user clicks the gamepad buttons. You can find values such as GamepadA and GamepadB in the VirtualKey enumeration so basic method for checking their pressing could look like this:

private void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
{
    if (args.Handled)
    {
        return;
    }

    switch (args.VirtualKey)
    {
        case VirtualKey.GamepadA:
            // Gamepad A button was pressed
            break;

        case VirtualKey.GamepadB:
            // Gamepad B button was pressed
            break;

        case VirtualKey.GamepadX:
            // Gamepad X button was pressed
            break;

        case VirtualKey.GamepadY:
            // Gamepad Y button was pressed
            break;
    }
}

You have to subscribe the event (for example in constructor):

Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
Marian Dolinský
  • 3,312
  • 3
  • 15
  • 29
  • Hmm ok. I'll try that as well. Which of mine or your approaches is the generally accepted one? (I've still not tested my approach yet). – Lee Englestone Jan 24 '17 at 14:51
  • I think your approach is too complicated for just getting the info whether the button were tapped. It will be more useful in games and such things I think.. – Marian Dolinský Jan 24 '17 at 19:13
  • I'm wondering if that worked for the xbox as it required inclusion of the Window.System lib – David Bollman Jun 03 '19 at 23:37