Imports System Imports System.Windows.Forms Imports Microsoft.DirectX.DirectInput Imports Microsoft.DirectX Public Class Form1 Public Shared state As New JoystickState() Private applicationDevice As Device = Nothing Private Sub MainClass_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not InitDirectInput() Then Close() End If Timer1.Start() CheckForIllegalCrossThreadCalls = False End Sub Public Function InitDirectInput() As Boolean Dim instance As DeviceInstance For Each instance In Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly) applicationDevice = New Device(instance.InstanceGuid) Exit For Next instance If (Nothing Is applicationDevice) Then MessageBox.Show("Unable to create a joystick device. Sample will exit.", "No joystick found") Return False End If ' Set the data format to the c_dfDIJoystick pre-defined format. applicationDevice.SetDataFormat(DeviceDataFormat.Joystick) ' Set the cooperative level for the device. applicationDevice.SetCooperativeLevel(Me, CooperativeLevelFlags.Exclusive Or CooperativeLevelFlags.Foreground) ' Enumerate all the objects on the device. Dim d As DeviceObjectInstance For Each d In applicationDevice.Objects ' For axes that are returned, set the DIPROP_RANGE property for the ' enumerated axis in order to scale min/max values. If 0 <> (d.ObjectId And CInt(DeviceObjectTypeFlags.Axis)) Then ' Set the range for the axis. applicationDevice.Properties.SetRange(ParameterHow.ById, d.ObjectId, New InputRange(-10, +10)) End If Next d Return True End Function 'InitDirectInput Private Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick GetData() End Sub Public Sub GetData() ' Make sure there is a valid device. If Nothing Is applicationDevice Then Return End If Try ' Poll the device for info. applicationDevice.Poll() Catch inputex As InputException If TypeOf inputex Is NotAcquiredException Or TypeOf inputex Is InputLostException Then ' Check to see if either the app ' needs to acquire the device, or ' if the app lost the device to another ' process. Try ' Acquire the device. applicationDevice.Acquire() Catch ' Failed to acquire the device. ' This could be because the app ' doesn't have focus. Return End Try End If End Try 'catch(InputException inputex) ' Get the state of the device. Try state = applicationDevice.CurrentJoystickState ' Catch any exceptions. None will be handled here, ' any device re-aquisition will be handled above. Catch Return End Try UpdateUI() End Sub 'GetData Public Sub UpdateUI() Dim button As Integer = 0 Dim strText As String = Nothing Dim b As Byte Dim buttons As Byte() = state.GetButtons() For Each b In buttons If 0 <> (b And &H80) Then strText += button.ToString("00 ") End If button += 1 Next b TextBox1.Text = state.X.ToString() TextBox3.Text = state.Y.ToString() End Sub 'UpdateUI Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Close() End Sub Protected Overloads Sub Dispose(ByVal disposing As Boolean) Timer1.Stop() ' Unacquire all DirectInput objects. If Not Nothing Is applicationDevice Then applicationDevice.Unacquire() End If If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub End Class 'MainClass