First, download DirectX SDK from Microsoft:
http://msdn.microsoft.com/en-us/directx/aa937788.aspx
and then install it.
Starts a new project on VB.NET and add a reference to DirectX:
Project -> Add reference.
Select Microsoft.DirectX.DirectInput from list in ".NET" tab:
First thing to do is obtain a device list, but VB.NET will throw an exception (Loader Lock). Deactivate Loader Lock exception from menu Debud -> Exceptions. Explode "Managed Debugging Assistant", find "Loader Lock" and uncheck the box "Generated":
Imports DirectInput name space at top of your program:
Add a timer (timer1) on your form and set "Enabled" property on true (we'll use it for joystick polling).
Add a textbox named txtDir
Add a textbox named txtButtons
The code is the following:
Imports Microsoft.DirectX.DirectInput Public Class Form1 Dim joystickDevice As Device Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' List of attached joysticks Dim gameControllerList As DeviceList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly) ' There is one controller at least? If (gameControllerList.Count > 0) Then ' Select first joystick gameControllerList.MoveNext() ' Create an object instance Dim deviceInstance As DeviceInstance = gameControllerList.Current joystickDevice = New Device(deviceInstance.InstanceGuid) joystickDevice.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive) End If joystickDevice.SetDataFormat(DeviceDataFormat.Joystick) joystickDevice.Acquire() Timer1.Start() End Sub Private Sub joystickPolling() Try joystickDevice.Poll() Dim state As JoystickState = joystickDevice.CurrentJoystickState txtDir.Text = "X=" & state.X & " Y=" & state.Y & " Z=" & state.Z txtbuttons.Text = state.GetButtons(0) & " " & state.GetButtons(1) Catch ex As Exception End Try End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick joystickPolling() End Sub End Class
state object will contain all informations you need. State.X is the X position of joystick (type state followed by dot and see the entire list of controls).
the GetButtons property of state object will give you the state of buttons. GetButtons give an array.
Example:
dim button1 as byte = state.GetButtons(0) ' retrieve first button state
if the value is 128, the button is pressed, if the value is 0 the button isn't pressed or doesn't exists