如何更新操纵杆状态



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

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

最后一行获取操纵杆的状态。我已经看到使用了计时器,每次它滴答时状态都会刷新,但这似乎并不有效,因为如果我不按任何按钮,状态无论如何都会刷新。那么,如何在需要时才刷新状态呢?

我发现了一些c#代码,说明如何仅在操纵杆状态发生变化时更新操纵杆状态。我看到你有:

Dim state As JoystickState = js.CurrentJoystickState

也许可以试着添加一些像下面的例子:"THIS IS 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;
    }

最新更新