Search code examples
.netvb.netjoystick

VB 2010 Direct input with GamePad


I have a form with 2 labels on it, the first label displays the USB game pads name (once found) the second i want to display the button pushed, here's what i have so far:

 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

End Class

All i see are 0's on the output, which means the buttons being pushed on the keypad arent being detected

Anyone know how i can resolve this issue?


Solution

  • Funny enough, it's exactly like the error says: you need to acquire the device before beginning to poll it.

    _device.Acquire();
    

    Note that this only happens once before the actual polling function.