Tutorial by Nicholas Gorski (GMan)
This tutorial will use the Windows API to get joystick input.
Here is a list of what functions and type's we will need. I suggest adding a module or class to your project and adding these there.
Public Declare Function joyGetPosEx Lib "winmm.dll" (ByVal uJoyID As Long, _
pji As JOYINFOEX) As Long
Public Declare Function joyGetDevCapsA Lib "winmm.dll" (ByVal uJoyID As Long, _
pjc As JOYCAPS, ByVal cjc As Long) As Long
joyGetPosEx returns input information.
joyGetDevCapsA returns information on the joystick.
Public Type JOYCAPS
wMid As Integer wPid As Integer szPname As String * 32 wXmin As Long wXmax As Long wYmin As Long wYmax As Long wZmin As Long wZmax As Long wNumButtons As Long wPeriodMin As Long wPeriodMax As Long wRmin As Long wRmax As Long wUmin As Long wUmax As Long wVmin As Long wVmax As Long wCaps As Long wMaxAxes As Long wNumAxes As Long wMaxButtons As Long szRegKey As String * 32 szOEMVxD As String * 260
Public Type JOYINFOEX
dwSize As Long dwFlags As Long dwXpos As Long dwYpos As Long dwZpos As Long dwRpos As Long dwUpos As Long dwVpos As Long dwButtons As Long dwButtonNumber As Long dwPOV As Long dwReserved1 As Long dwReserved2 As Long
Other public variables
Public JoyNum As Long
Public MYJOYEX As JOYINFOEX
Public MYJOYCAPS As JOYCAPS
Public CenterX As Long
Public CenterY As Long
Public JoyButtons(15) as Boolean
Public CurrentJoyX As Long
Public CurrentJoyY As Long
Coding the Input
Create a function named StartJoystick. All this does is get the CAPS and Center positions:
Public Function StartJoystick(Optional ByVal JoystickNumber As Long = 0) As Boolean
JoyNum = JoystickNumber
If joyGetDevCapsA(JoyNum, MYJOYCAPS, 404) <> 0 Then 'Get joystick info
StartJoystick = False
Call joyGetPosEx(JoyNum, MYJOYEX) CenterX = MYJOYEX.dwXpos CenterY = MYJOYEX.dwYpos StartJoystick = True
End If End Function
As you can see, it's not too complicated. The JoystickNumber variable is what joystick you want to get input from. This is usually zero, but can be one [or two for hardcore users...:)].
Next, add a function called PollJoystick. This function will get all the information from the joystick, then sort it out:
Public Sub PollJoystick()
Dim i As Long
Dim t As Long
MYJOYEX.dwSize = 64 MYJOYEX.dwFlags = 255
' Get the joystick information Call joyGetPosEx(JoyNum, MYJOYEX)
t = MYJOYEX.dwButtons For i = 15 To 0 Step -1
JoyButtons(i) = False If (2 ^ i) <= t Then t = t - (2 ^ i) JoyButtons(i) = True End If
CurrentJoyX = MYJOYEX.dwXpos CurrentJoyY = MYJOYEX.dwYpos End Sub
Now you have the ability to get input from the joystick. But how do you use it? And what's with the complicated code? Well, that complicated code is what tells you what buttons are pressed. How? When you press button zero on your joystick, the joystick tells the computer 1. Press one, it says 2. But press two, it says four. Press three, and its 8. See the pattern? If not, here it is:
See? So press buttons zero, two and three, and the joystick tells the computer 11. How do we sort this out? Start with the highest number, then goto zero. If the current number is within the input number (11 in our example), then subtract that from from the number. So in our example, it would get the 2^3=8, so subtract it, leaving us with 3. Then it gets to 2^1=2, subtract it, leaving 1. So then 2^0=1, subtract it, leaving zero. Get it? If not, trust me (I'm bad at explaining algorithms).
Download the source code to see it in action, in a stupid little game.