Results 1 to 4 of 4

Thread: [RESOLVED] joystick object

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007

    Resolved [RESOLVED] joystick object

    Can any one help me...
    I'm build a joystick object... here i want the directions(8) and buttons(16)...
    i found this code in internet. i don't hunderstand every thing, because i don't know how the joystick is work. i try change the code... the Visual Basic don't
    give me a error, but when i compare the variales valor the visual basic ignore some code... heres the code:

    'Module Joystick
    Option Explicit
    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
    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
    End Type
    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
    End Type
    Public JoyNum As Long
    Public CenterX As Long
    Public CenterY As Long
    Public JoyButtons(15) As Boolean
    Public CurrentJoyX As Long
    Public CurrentJoyY As Long
    Public lngButton As Long
    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
        MYJOYEX.dwSize = 64
        MYJOYEX.dwFlags = 255
        Call joyGetPosEx(JoyNum, MYJOYEX)
        CenterX = MYJOYEX.dwXpos
        CenterY = MYJOYEX.dwYpos
        StartJoystick = True
    End If
    End Function
    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
    Next i
    CurrentJoyX = MYJOYEX.dwXpos
    CurrentJoyY = MYJOYEX.dwYpos
    lngButton = t
    End Sub
    I change one thing from the original code: i put one public variavel for see the button that is pressed...
    'code for form load
    Private Sub Form_Load()
    If Not StartJoystick Then
        MsgBox "No joystick!"
    End If
    End Sub
    'now the code for the timer
    Private Sub Timer_Timer()
    'Gather Input
    Call PollJoystick
    If CurrentJoyX > CenterX Then
        MsgBox ("Right")
    ElseIf CurrentJoyX < CenterX Then
        MsgBox ("Left")
    ElseIf CurrentJoyY < CenterY Then
        MsgBox ("Up")
    ElseIf CurrentJoyY > CenterY Then
        MsgBox ("Down")
    ElseIf (CurrentJoyX < CenterX And CurrentJoyY < CenterY) Then
        MsgBox ("LeftUp") 'diagonal LeftUp
    ElseIf (CurrentJoyX < CenterX And CurrentJoyY > CenterY) Then
        MsgBox ("LeftDown")
    ElseIf (CurrentJoyX > CenterX And CurrentJoyY < CenterY) Then
        MsgBox ("RightUp")
    ElseIf (CurrentJoyX > CenterX And CurrentJoyY > CenterY) Then
        MsgBox ("RightDown")
    End If
    If lngButton = 1 Then
        MsgBox ("Button1")
    End If
    End Sub
    Some times de diagnal direcions work, but the buttons no.... try teste the code and then tell me something....
    if you need more information/code, just tell me... [Email Removed]
    Thank you.....
    Last edited by RobDog888; Jan 5th, 2008 at 01:38 PM. Reason: Removed email address to help protect the member from spam

  2. #2
    Frenzied Member
    Join Date
    Mar 2006

    Re: joystick object

    It looks like this is over your head but if this isn't working for you, you can try using DirectInput.

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007

    Re: joystick object

    thank you... but i did the joystick with out DirectInput.
    i don't know why the variables centerx, centery and others was ignors... but i used the type variables(these variables had the valor)....
    know i have the object and its work fine...
    note: i'm sorry if my english is confuse...

  4. #4
    Join Date
    Apr 2009

    Re: joystick object

    Could you please post the DirectInput code?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width