0

I'm developing an UWP app for XBOX using the WebView method on VS2022.

I'm using the addEventListener method to listen for all the keyDown events that are getting triggered when any button is pressed on the XBOX controller.

But the keydown events are not getting triggered at all when I launch the app and to access this keyDown events I need to click on the Controller X icon and close the menu bar after doing this the events are triggered properly.

JS Code Snippet:

document.addEventListener(
"keydown",
ev => {
  if (ev.keyCode === 196) {
    ev.preventDefault();   // I'm using this to prevent the default back functionality of xbox.
  }
},
false
);

UWP Code Snippet:

public App()
    {
        this.InitializeComponent();
        this.RequiresPointerMode = ApplicationRequiresPointerMode.WhenRequested;   // used this method to remove the mouse mode on XBOX.
        this.Suspending += OnSuspending;
    }

Is there anyway that can be used to trigger the keyDown events when the app is launched.

Thanks in advance. Happy Coding!!!

1 Answers1

0

After making some research and going through all the microsoft docs for the XBOX development I found one of the Stack Overflow question which mentioned the same issue but in a different scenario and used that method to get a fix for this in this SO link Why does XAML WebView Input Box not have cursor until I open/close Guide on XBox One? and the comment that has been mentioned in this post Navigation using Controller doesn't work after turning on the controller

There the same issue has been observed on the input box whereas in our scenario it is with the addEventListener.

The main root cause which is causing this issue is that the focus is not being set to the WebView when the app is launched. Once I open the XBOX menu guide and close, it is setting the focus on to the WebView which is then able to listen to the events.

To set the focus on to the WebView we need to add some part of the code explicitly in your code-behind which will help in setting the focus when the app is launched.

private void MyWebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
    {
        MyWebView.Visibility = Visibility.Collapsed;
        MyWebView.Visibility = Visibility.Visible;
        MyWebView.Focus(FocusState.Keyboard);  //here I'm using the keyboard method which worked for me we can go with programmatic method as well.
    }

Here in the code-behind file we're using the NavigationCompleted which will be called once the WebView is successfully loaded the required content and then we're making the Visibility to collapsed and visible again which will help in setting the focus on to the WebView and listen to the keyDown Events.

Hope this helps and Happy Coding!!!