Search code examples
javascriptuwpxbox

UWP - Xbox One - JavaScript - disable gamepadB default back navigation behaviour


I have a pure JavaScript/React bundled pwa/hosted web app running on Xbox One. The app has its own custom navigation. I want to disable gamepadB default history.goBack() behaviour.

As per

xbox one controller prevent default back button behavior with Javascript

something like this should work, but it doesn't:

navigator.gamepadInputEmulation = "keyboard";
if (typeof Windows !== "undefined") {
  const systemNavManager = Windows.UI.Core.SystemNavigationManager.getForCurrentView();

  if (systemNavManager) {
    systemNavManager.addEventListener(
      'backrequested',
      event => {
        event.handled = true;
      },
      false
    );
  }
}

setting event.handled in capture/bubble phase does nothing. event.handled is already set to true before this handler is called. Using:

event.preventDefault()

will crash the app as standard dom functionalities like preventDefault, stopPropagation, and stopImmediatePropagation are not supported.

BackRequestedEventArgs Class | Windows Dev Center


Solution

  • figured it out, just adding e.preventDefault() on a keydown event where the keyCode is gamepadB will stop the default system back navigation

    navigator.gamepadInputEmulation = "keyboard";
    
    document.addEventListener('keydown', e => {
                if (e.keyCode === 196 // 196 is the keycode for B button on gamepad) {
    
                    e.preventDefault();
                    this.handleBack(e);
                }
            });