如何在按下XBOX控制器的GamepadB按钮时实现后退导航

huangapple go评论86阅读模式
英文:

How can I implement the back navigation when clicked on GamepadB button on XBOX controller

问题

Gamepad的B按钮会暂停应用而不是进行后退导航。

我尝试开发一个使用WebView的UWP应用,但当我尝试通过点击Gamepad上的“B”按钮来在应用中进行后退导航时,整个应用都会暂停,并返回到主页。是否有任何方法可以实现XBOX控制器的后退键功能呢?

例如,如果我在Netflix主页上,如果我点击任何节目,它将导航到该内容的详细信息页面。然后,如果我点击后退,它应该导航回主页,而不是暂停整个应用。

Mainpage

<WebView Source="https://luser.github.io/gamepadtest/">
</WebView>

这是我到目前为止使用的WebView部分,并根据文档中提供的GoBack()功能进行了一些更改,但仍然面临着后退导航根本不起作用的问题。

'MainPage.xaml.cs'

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}

private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
    if(args.VirtualKey == Windows.System.VirtualKey.GamepadB)
    {
        if(source.CanGoBack)
        {
            source.GoBack();
        }
    }
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    Window.Current.CoreWindow.KeyDown -= CoreWindow_KeyDown;
}

我是否漏掉了导致此问题的任何内容,或者是否有任何其他方法,我必须使用它来获得所需的功能?

我设置了 **navigation.gamepadInputEmulation = 'keyboard':**

提前感谢您抽出时间查看问题并帮助我。

英文:

Gamepad B button is suspending the app instead of back navigation.

I'm trying to develop a UWP app using the WebView but when I try to do the back navigation in the app by clicking the "B" button on the Gamepad it is suspending the whole app and returning to the home page is there anyway how I can implement the back key functionality for the XBOX controller.

For example if I'm in the Netflix home page and if I click on any show it'll navigate me to the details page of that content. From there if I click on back it should navigate back to the home page instead of suspending the whole app.

Mainpage

<WebView Source="https://luser.github.io/gamepadtest/">
</WebView>

This is the webview part that I've used till now and made some changes by using the GoBack() functionality provided in the documentation but still facing the same issue like the back navigation is not working at all.

'MainPage.xaml.cs'

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}

private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
    if(args.VirtualKey == Windows.System.VirtualKey.GamepadB)
    {
        if(source.CanGoBack)
        {
            source.GoBack();
        }
    }
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    Window.Current.CoreWindow.KeyDown -= CoreWindow_KeyDown;
}

Am I missing anything which is causing this issue or is there any other method that I've to use to get the required functionality.

I'm setting the **navigation.gamepadInputEmulation = 'keyboard':**

Thank you in advance for taking your time to look at the question and helping me out.

答案1

得分: 1

I've tried with some other methods that has been provided in the documentation and Forums but none of them has worked for me. So, here's the solution which worked for me well.

我尝试了文档和论坛中提供的一些其他方法,但都没有对我起作用。因此,这是对我有效的解决方案。

I've added one event listener to capture the button click events in the XBOX controller instead of having a loop to check which button is pressed I've replaced it with "document.addEventListener" which will not give any events when tried on the Browser but will be able to capture the keyCodes when run in XBOX console.

我添加了一个事件侦听器来捕获XBOX控制器中的按钮点击事件,而不是使用循环来检查哪个按钮被按下,我用"document.addEventListener"来替代它,当在浏览器上尝试时不会触发任何事件,但在XBOX控制台上运行时能够捕获键码。

Here, when we use the addEventListener the keyCodes will differ from the actual keycodes that are provided in the documentation. Here, we'll be getting the keycode as '196' for GamepadB button. After adding this line able to disable the default functionality.

在这里,当我们使用addEventListener时,键码将与文档中提供的实际键码不同。在这里,我们将获得GamepadB按钮的键码为'196'。添加了这一行后,能够禁用默认功能。

if (ev.keyCode === 196) {
ev.preventDefault();
}

Hope this helps someone in the future. Happy coding!!!

希望这能帮助将来的某人。愉快的编程!

英文:

I've tried with some other methods that has been provided in the documentation and Forums but none of them has worked for me. So, here's the solution which worked for me well.

I've added one event listener to capture the button click events in the XBOX controller instead of having a loop to check which button is pressed I've replaced it with "document.addEventListener" which will not give any events when tried on the Browser but will be able to capture the keyCodes when run in XBOX console.

Here, when we use the addEventListener the keyCodes will differ from the actual keycodes that are provided in the documentation. Here, we'll be getting the keycode as '196' for GamepadB button. After adding this line able to disable the default functionality.

if (ev.keyCode === 196) {
    ev.preventDefault();
  }

Hope this helps someone in the future. Happy coding!!!

huangapple
  • 本文由 发表于 2023年5月29日 19:43:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定