简体   繁体   English

捕捉游戏手柄按键的可靠方式(非游戏应用程序)

[英]Reliable way to capture gamepad button press (non-game application)

I am trying to use a gamepad to control an application. 我正在尝试使用游戏手柄来控制应用程序。 It's not a game, just a plain application using Windows Forms. 它不是游戏,只是使用Windows Forms的普通应用程序。 So it doesn't have a game loop/update process or anything like that. 所以它没有游戏循环/更新过程或类似的东西。 I wouldn't like to use XNA because I think it's a huge overload just to capture a button press. 我不想使用XNA,因为我认为捕获按钮是一个巨大的过载。

I am experimenting with both SlimDX and SharpDX. 我正在尝试SlimDX和SharpDX。 As I understand, they are just wrappers for DirectX, right? 据我所知,它们只是DirectX的包装,对吧?

Looking at the documentation, it seems like there is no event for a button press. 查看文档,似乎按钮按下没有事件。 So I have been looking for an alternative. 所以我一直在寻找替代方案。 I have tried adding a timer (from the System.Windows.Forms.Timer class), and reading the state of the gamepad like this: 我尝试添加一个计时器(来自System.Windows.Forms.Timer类),并读取游戏手柄的状态,如下所示:

private void timer_tick(object sender, EventArgs e)
{
    State s = controller.GetState();
    stateLabel.Text = s.Gamepad.Buttons == GamepadButtonFlags.A ? "A" : "";
}

With a small enough interval between timer ticks (I'm using 10ms), I can see whether the button is pressed or not. 在计时器滴答之间有足够小的间隔(我使用10ms),我可以看到按钮是否被按下。 However, I don't want to handle the button press multiple times should the button be held down - I need to make sure it has been released before handling the button press again. 但是,如果按下按钮,我不想多次按下按钮 - 我需要确保在再次按下按钮之前已经释放按钮。 Between two ticks of the timer, I don't know if a button was pressed twice or if it was just being held down. 在计时器的两个刻度之间,我不知道是否按下了两次按钮或是否只是按下按钮。

I thought about using the packet number in the controller state, but it will change at the slightest touch on an analog stick or shoulder trigger. 我考虑过在控制器状态下使用数据包编号,但它会在模拟摇杆或肩部触发器上轻微触摸时发生变化。

Help? 救命?

From what I can gather after reading you question over a few times is that you are wanting to log a button press only 1 time until it is released. 从我可以收集的内容中读出几次问题后,您只想按一下按钮直到它被释放。 You probably should use the packet number technique to keep track of any changes to the other input. 您可能应该使用数据包编号技术来跟踪对其他输入的任何更改。

To get a single input from the button being held down as opposed to getting continuous input (1 as opposed to 11111111...etc) create an old state an a current state. 要从按下按钮获得单个输入而不是获得连续输入(1而不是11111111 ......等),请创建旧状态和当前状态。 Then compare the old state with the new state. 然后将旧状态与新状态进行比较。

Something like this: 像这样的东西:

class Input
{
    State old;

    void GetInput()
    {
        State new = controller.GetState();
        if (this.old.GamePad.Buttons == GamepadButtonFlags.A && new.GamePad.Buttons == GamepadButtonFlags.A)
        {
            // Do stuff that will be called only once.
        }
        this.old = new;
    }
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM