(на форме таймер)
- Код: Выделить всё
Option Explicit
Private Declare Function joyGetPosEx Lib "winmm.dll" (ByVal uJoyID As Long, _
pji As JOYINFOEX) As Long
Private Declare Function joyGetDevCapsA Lib "winmm.dll" (ByVal uJoyID As Long, _
pjc As JOYCAPS, ByVal cjc As Long) As Long
Private 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
End Type
Private 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
End Type
Private JoyNum As Long
Private MYJOYEX As JOYINFOEX
Private MYJOYCAPS As JOYCAPS
Private CenterX As Long
Private CenterY As Long
Private JoyButtons(15) As Boolean
Private CurrentJoyX As Long
Private CurrentJoyY As Long
Private Function StartJoystick(Optional ByVal JoystickNumber As Long = 0) As Boolean
JoyNum = JoystickNumber
If joyGetDevCapsA(JoyNum, MYJOYCAPS, 404) <> 0 Then 'Get joystick info
StartJoystick = False
Else
Call joyGetPosEx(JoyNum, MYJOYEX)
CenterX = MYJOYEX.dwXpos
CenterY = MYJOYEX.dwYpos
StartJoystick = True
End If
End Function
Private 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
Next i
CurrentJoyX = MYJOYEX.dwXpos
CurrentJoyY = MYJOYEX.dwYpos
End Sub
Private Sub Form_Load()
If StartJoystick = False Then
MsgBox "NO JOYSTICK"
Unload Me ' err
End If
Timer1.Interval = 100
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim i As Integer
PollJoystick
Me.Cls
For i = 0 To UBound(JoyButtons)
If JoyButtons(i) = True Then Me.Print Str(i) & " ON" Else Me.Print Str(i) & " OFF"
Next i
Print "x:" & Str(CurrentJoyX)
Print "y:" & Str(CurrentJoyY)
End Sub
Но хотелось бы как-нибудь уменьшить потребление ресурсов при опросе джойстика - имхо таймер здесь не самое рациональное использование ресурсов.. Вроде были какие-то функции захвата джойстика на определенное окно и какие-то встроенные в драйвер способы опроса.. - кто работал с этим подскажите плиз, надо чтобы как можно меньше грузило процессор.
Заранее спасибо.
p.s. что лучше - иметь глобальный булевский массив из 15 кнопок и перезаполнять его много раз в секунду, или каждый раз объявлять одноразовый массив в самой процедуре?