如何更新操纵杆状态

时间:2011-12-21 19:32:25

标签: .net wpf vb.net

我想每次按下操纵杆状态或移动操纵杆的轴时更新操纵杆状态。我已经有了这个代码:

Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectInput

 Dim js As Device = Nothing

 For Each DI As DeviceInstance In Manager.GetDevices(DeviceClass.GameControl, _
     EnumDevicesFlags.AttachedOnly)

     js = New Device(DI.InstanceGuid)
     Exit For
 Next

 If js Is Nothing Then
     Throw New Exception("No joystick found")
 End If

 Dim wih As New System.Windows.Interop.WindowInteropHelper(Me)
 js.SetCooperativeLevel(wih.Handle, CooperativeLevelFlags.NonExclusive Or _
     CooperativeLevelFlags.Background)
 js.Acquire()

 Dim state As JoystickState = js.CurrentJoystickState

最后一行获取操纵杆的状态。我已经看到使用了一个计时器,并且每次打勾都会刷新状态,但它看起来效率不高,因为如果我不按任何按钮,状态就会被刷新。那么如何在需要时才刷新状态呢?

1 个答案:

答案 0 :(得分:0)

我发现了一些关于如何在操纵杆状态发生变化时更新操纵杆状态的C#代码。我知道你有:

Dim state As JoystickState = js.CurrentJoystickState

也许只是尝试添加类似下面的示例:“这是C#”

            //Capture Position.
            info += "X:" + state.X + " ";
            info += "Y:" + state.Y + " ";
            info += "Z:" + state.Z + " ";

            //Capture Buttons.
            byte[] buttons = state.GetButtons();
            for(int i = 0; i < buttons.Length; i++)
            {
                if(buttons[i] != 0)
                {
                    info += "Button:" + i + " ";
                }
            }

以下是我找到的整个代码段:https://msdn.microsoft.com/en-us/library/windows/desktop/bb153252%28v=vs.85%29.aspx#dx_DirectInput_capturing_device_objects

private void UpdateJoystick()
    {
        string info = "Joystick: ";

        //Get Mouse State.
        JoystickState state = joystick.CurrentJoystickState;

        //Capture Position.
        info += "X:" + state.X + " ";
        info += "Y:" + state.Y + " ";
        info += "Z:" + state.Z + " ";

        //Capture Buttons.
        byte[] buttons = state.GetButtons();
        for(int i = 0; i < buttons.Length; i++)
        {
            if(buttons[i] != 0)
            {
                info += "Button:" + i + " ";
            }
        }

        lbJoystick.Text = info;
    }