【问题标题】:How to detect game controller button presses (ABXY) in UWP apps如何检测 UWP 应用程序中的游戏控制器按钮按下 (ABXY)
【发布时间】:2017-01-24 04:04:01
【问题描述】:

我开发了一个可以在 XBOX 上运行的 UWP 应用。

我想检测游戏手柄控制器上的按钮是否被按下(A B X 或 Y)。

我认为我需要使用点击事件?如果是在点击事件上,我需要在点击事件中签到什么?

查看这篇文章,确定是否按下了触发器..

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
}

我认为有一种等效的方法可以检查是否按下了 ABXY 按钮之一?。下次有机会我会检查的。

另一方面,这篇博文看起来对刚开始为 Xbox One 开发 UWP 的人非常有用 http://grogansoft.com/blog/?p=1278

更新: 看起来我可以调用 GetCurrentReading() 来获得 GamepadReading 结构。并从中获取GamepadButtons 的状态。

【问题讨论】:

    标签: c# uwp xbox-one


    【解决方案1】:

    来自CoreWindowKeyDown 事件或任何其他 UWP 控件即使在用户单击游戏手柄按钮时也会被触发。您可以在 VirtualKey 枚举中找到诸如 GamepadAGamepadB 之类的值,因此检查它们的按下的基本方法如下所示:

    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;
        }
    }
    

    您必须订阅事件(例如在构造函数中):

    Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
    

    【讨论】:

    • 嗯好的。我也会试试的。我或您的哪种方法是普遍接受的? (我还没有测试我的方法)。
    • 我认为您的方法过于复杂,无法仅获取按钮是否被点击的信息。我认为它在游戏和类似的事情中会更有用..
    • 我想知道这是否适用于 xbox,因为它需要包含 Window.System 库
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多