Attribute VB_Name = "Joystick" Option Explicit Public Const joyMax As Long = &HFFF Public X As Long, Y As Long, Z As Long #Const WINVER = &H400 Public Const MAXPNAMELEN = 32 ' max product name length (including NULL) Public Const MAX_JOYSTICKOEMVXDNAME = 260 ' max oem vxd name length (including NULL) 'constants used with JOYINFO and JOYINFOEX structures and MM_JOY* messages Public Const JOYERR_BASE = 160 Public Const JOYERR_NOERROR = (0) Public Const JOYERR_PARMS = (JOYERR_BASE + 5) Public Const JOYERR_NOCANDO = (JOYERR_BASE + 6) Public Const JOYERR_UNPLUGGED = (JOYERR_BASE + 7) 'constants used with JOYINFO and JOYINFOEX structures and MM_JOY* messages Public Const JOY_BUTTON1 = &H1 Public Const JOY_BUTTON2 = &H2 Public Const JOY_BUTTON3 = &H4 Public Const JOY_BUTTON4 = &H8 Public Const JOY_BUTTON1CHG = &H100 Public Const JOY_BUTTON2CHG = &H200 Public Const JOY_BUTTON3CHG = &H400 Public Const JOY_BUTTON4CHG = &H800 'constants used with JOYINFOEX Public Const JOY_BUTTON5 As Long = &H101 Public Const JOY_BUTTON6 As Long = &H201 Public Const JOY_BUTTON7 As Long = &H401 Public Const JOY_BUTTON8 As Long = &H801 Public Const JOY_BUTTON9 As Long = &H1001 Public Const JOY_BUTTON10 As Long = &H2001 Public Const JOY_BUTTON11 As Long = &H4001 Public Const JOY_BUTTON12 As Long = &H8001 Public Const JOY_BUTTON13 As Long = &H10001 Public Const JOY_BUTTON14 As Long = &H20001 Public Const JOY_BUTTON15 As Long = &H40001 Public Const JOY_BUTTON16 As Long = &H80001 Public Const JOY_BUTTON17 As Long = &H100001 Public Const JOY_BUTTON18 As Long = &H200001 Public Const JOY_BUTTON19 As Long = &H400001 Public Const JOY_BUTTON20 As Long = &H800001 Public Const JOY_BUTTON21 As Long = &H1000001 Public Const JOY_BUTTON22 As Long = &H2000001 Public Const JOY_BUTTON23 As Long = &H4000001 Public Const JOY_BUTTON24 As Long = &H8000001 Public Const JOY_BUTTON25 As Long = &H10000001 Public Const JOY_BUTTON26 As Long = &H20000001 Public Const JOY_BUTTON27 As Long = &H40000001 Public Const JOY_BUTTON28 As Long = &H80000001 'Public Const JOY_BUTTON29 As Long = &h100000001 'Public Const JOY_BUTTON30 As Long = &H200000001 'Public Const JOY_BUTTON31 As Long = &H400000001 'Public Const JOY_BUTTON32 As Long = &H800000001 'constants used with JOYINFOEX structure Public Const JOY_POVBACKWARD = 18000 Public Const JOY_POVCENTERED = -1 Public Const JOY_POVFORWARD = 0 Public Const JOY_POVLEFT = 27000 Public Const JOY_POVRIGHT = 9000 Public Const JOY_RETURNX = &H11 Public Const JOY_RETURNY = &H21 Public Const JOY_RETURNZ = &H41 Public Const JOY_RETURNR = &H81 'Rudder Public Const JOY_RETURNU = &H101 'axis 5 Public Const JOY_RETURNV = &H201 'axis 6 Public Const JOY_RETURNPOV = &H401 Public Const JOY_RETURNBUTTONS = &H801 Public Const JOY_RETURNRAWDATA = &H1001 Public Const JOY_RETURNPOVCTS = &H2001 Public Const JOY_RETURNCENTERED = &H4001 Public Const JOY_USEDEADZONE = &H8001 Public Const JOY_RETURNALL = (JOY_RETURNX Or JOY_RETURNY Or JOY_RETURNZ Or _ JOY_RETURNR Or JOY_RETURNU Or JOY_RETURNV Or _ JOY_RETURNPOV Or JOY_RETURNBUTTONS) Public Const JOY_CAL_READALWAYS = &H100001 Public Const JOY_CAL_READXYONLY = &H200001 Public Const JOY_CAL_READ3 = &H400001 Public Const JOY_CAL_READ4 = &H800001 Public Const JOY_CAL_READXONLY = &H1000001 Public Const JOY_CAL_READYONLY = &H2000001 Public Const JOY_CAL_READ5 = &H4000001 Public Const JOY_CAL_READ6 = &H8000001 Public Const JOY_CAL_READZONLY = &H10000001 Public Const JOY_CAL_READRONLY = &H20000001 Public Const JOY_CAL_READUONLY = &H40000001 Public Const JOY_CAL_READVONLY = &H80000001 'joystick ID constants Public Const JOYSTICKID1 = 0 Public Const JOYSTICKID2 = 1 'joystick device capabilities data structure Public Type JOYCAPS wMid As Integer ' manufacturer ID wPid As Integer ' product ID szPname As String * MAXPNAMELEN ' product name (NULL terminated string) wXmin As Integer ' minimum x position value wXmax As Integer ' maximum x position value wYmin As Integer ' minimum y position value wYmax As Integer ' maximum y position value wZmin As Integer ' minimum z position value wZmax As Integer ' maximum z position value wNumButtons As Integer ' number of buttons wPeriodMin As Integer ' minimum message period when captured wPeriodMax As Integer ' maximum message period when captured #If WINVER >= &H400 Then wRmin As Integer ' minimum r position value wRmax As Integer ' maximum r position value wUmin As Integer ' minimum u (5th axis) position value wUmax As Integer ' maximum u (5th axis) position value wVmin As Integer ' minimum v (6th axis) position value wVmax As Integer ' maximum v (6th axis) position value wCaps As Integer ' joystick capabilites wMaxAxes As Integer ' maximum number of axes supported wNumAxes As Integer ' number of axes in use wMaxButtons As Integer ' maximum number of buttons supported szRegKey As String * MAXPNAMELEN ' registry key szOEMVxD As String * MAX_JOYSTICKOEMVXDNAME ' OEM VxD in use #End If End Type Public Type JOYINFO wXpos As Long wYpos As Long wZpos As Long wButtons As Long End Type Public Type JOYINFOEX dwSize As Long ' size of structure dwFlags As Long ' flags to indicate what to return dwXpos As Long ' x position dwYpos As Long ' y position dwZpos As Long ' z position dwRpos As Long ' rudder/4th axis position dwUpos As Long ' 5th axis position dwVpos As Long ' 6th axis position dwButtons As Long ' button states dwButtonNumber As Long ' current button number pressed dwPOV As Long ' point of view state dwReserved1 As Long ' reserved for communication between winmm driver dwReserved2 As Long ' reserved for future expansion End Type 'Functions Public Declare Function joyGetNumDevs Lib "winmm" () As Long Public Declare Function joyGetDevCaps Lib "winmm" Alias "joyGetDevCapsA" (ByVal id As Long, lpCaps As JOYCAPS, ByVal uSize As Long) As Long Public Declare Function joyGetPos Lib "winmm" (ByVal uJoyID As Long, pji As JOYINFO) As Long Public Declare Function joyGetPosEx Lib "winmm" (ByVal uJoyID As Long, pji As JOYINFOEX) As Long Public Declare Function joyGetThreshold Lib "winmm" (ByVal id As Long, lpuThreshold As Long) As Long Public Declare Function joyReleaseCapture Lib "winmm" (ByVal id As Long) As Long Public Declare Function joySetCapture Lib "winmm" (ByVal hwnd As Long, ByVal uID As Long, ByVal uPeriod As Long, ByVal bChanged As Long) As Long Public Declare Function joySetThreshold Lib "winmm" (ByVal id As Long, ByVal uThreshold As Long) As Long Public Declare Function GetLastError Lib "Kernel32" () As Long Private Num As Long Private Info As JOYINFOEX, Info2 As JOYINFO Private Config As JOYCAPS Public Sub InitJoystick() Dim Value As Long, Error As Long Num = joyGetNumDevs Error = GetLastError Debug.Assert Error = 0 Value = joyGetDevCaps(JOYSTICKID1, Config, LenB(Config)) Error = GetLastError Debug.Assert Error = 0 Debug.Print "Joystick: " & Trim$(Config.szPname) Info.dwSize = LenB(Info) If Config.wNumAxes <= 1 Then Debug.Print "Too Few!" Debug.Assert 0 End If End Sub Public Sub GetPos() Dim Value As Long Value = joyGetPosEx(JOYSTICKID1, Info) If Config.wNumAxes >= 1 Then X = (Info.dwXpos - Config.wXmin) * joyMax / Config.wXmax If Config.wNumAxes >= 2 Then Y = (Info.dwYpos - Config.wYmin) * joyMax / Config.wYmax If Config.wNumAxes >= 3 Then Z = (Info.dwZpos - Config.wZmin) * joyMax / Config.wZmax End Sub