VB 2010使用GamePad直接输入

时间:2011-07-26 14:31:54

标签: .net vb.net joystick

我有一个带有2个标签的表格,第一个标签显示USB游戏手柄名称(一旦找到),第二个我要显示推按钮,这是我到目前为止所拥有的:

 Imports Microsoft.DirectX.DirectInput

Public Class Form1
Public _device As Device
Public _state As JoystickState
Public arm As Boolean = True


Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim gameControllerList As DeviceList
    gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly)

    If (gameControllerList.Count > 0) Then

        Dim deviceInstance As DeviceInstance
        label.Text = "Found"
        For Each deviceInstance In gameControllerList
            _device = New Device(deviceInstance.InstanceGuid)
            label.Text = deviceInstance.InstanceName
            _device.SetDataFormat(DeviceDataFormat.Joystick)
            Exit For
        Next
    Else
        label.Text = "not found"
    End If
    output.Clear()
    _device.Acquire()

    Call Poll()
End Sub


Public Sub Poll()
    Dim buttons() As Byte
    Dim i As Integer = 0
    _device.Poll()
    _state = _device.CurrentJoystickState
    buttons = _state.GetButtons()
    Dim word As String
    word = BitConverter.ToString(buttons)
    output.AppendText(word)

End Sub

结束班

我看到的只是输出上的0,这意味着键盘上按下的按钮没有被检测到

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

有趣的是,它与错误完全相同:您需要在开始轮询之前获取设备。

_device.Acquire();

请注意,这仅在实际轮询功能之前发生一次。

相关问题