Results 1 to 8 of 8

Thread: Simplest way to read a joystick in VB6

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Simplest way to read a joystick in VB6

    This uses the Windows API.

    Below is the code. Place this code your form (Form1), and place a timer control (Timer1) and picturebox control (Picture1) in on the form. Set the timer interval property to 1 (1ms). Set the scalemode property for both the form and the picturebox to Pixels, and set the autoredraw property on both also to tru. Set the picturebox appearance property to flat, borderstyle to none, and fillstyle to solid.

    Code:
    Private Const JOY_RETURNBUTTONS As Long = &H80&
    Private Const JOY_RETURNCENTERED As Long = &H400&
    Private Const JOY_RETURNPOV As Long = &H40&
    Private Const JOY_RETURNPOVCTS As Long = &H200&
    Private Const JOY_RETURNR As Long = &H8&
    Private Const JOY_RETURNRAWDATA As Long = &H100&
    Private Const JOY_RETURNU As Long = &H10
    Private Const JOY_RETURNV As Long = &H20
    Private Const JOY_RETURNX As Long = &H1&
    Private Const JOY_RETURNY As Long = &H2&
    Private Const JOY_RETURNZ As Long = &H4&
    Private Const JOY_RETURNALL As Long = (JOY_RETURNX Or JOY_RETURNY Or JOY_RETURNZ Or JOY_RETURNR Or JOY_RETURNU Or JOY_RETURNV Or JOY_RETURNPOV Or JOY_RETURNBUTTONS)
    
    Private Type JOYINFOEX
        dwSize As Long ' size of structure
        dwFlags As Long ' flags to dicate 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
    
    Private Declare Function joyGetPosEx Lib "winmm.dll" (ByVal uJoyID As Long, ByRef pji As JOYINFOEX) As Long
    
    Dim JI As JOYINFOEX
    
    Const JNum As Long = 0
    'Set this to the number of the joystick that
    'you want to read (a value between 0 and 15).
    'The first joystick plugged in is number 0.
    'The API for reading joysticks supports up to
    '16 simultaniously plugged in joysticks.
    'Change this Const to a Dim if you want to set
    'it at runtime.
    
    Private Sub Form_Load()
    JI.dwSize = Len(JI)
    JI.dwFlags = JOY_RETURNALL
    End Sub
    
    Private Sub Timer1_Timer()
    Cls
    If joyGetPosEx(JNum, JI) <> 0 Then
        Print "Joystick #"; CStr(JNum); " is not plugged in, or is not working."
    Else
        With JI
            Print "X = "; CStr(.dwXpos)
            Print "Y = "; CStr(.dwYpos)
            Print "Z = "; CStr(.dwZpos)
            Print "R = "; CStr(.dwRpos)
            Print "U = "; CStr(.dwUpos)
            Print "V = "; CStr(.dwVpos)
            If .dwPOV < &HFFFF& Then Print "PovAngle = "; CStr(.dwPOV / 100) Else Print "PovCentered"
            Print "ButtonsPressedCount = "; CStr(.dwButtonNumber)
            Print "ButtonBinaryFlags = "; CStr(.dwButtons)
            Picture1.Cls
            Picture1.Circle (.dwXpos / &HFFFF& * (Picture1.Width - 1), .dwYpos / &HFFFF& * (Picture1.Height - 1)), 2
        End With
    End If
    End Sub
    Then run it. If you have a joystick plugged in it will show all the values for the joysticks controls (all axes, all buttons, and POV hat). All controls that are not supported by the device remain zero. If the joystick with the set number isn't plugged in (or doesn't work), then an error message will display. It will immediately start displaying joystick data though the moment a working josystick is plugged in.
    Last edited by Ben321; Jun 17th, 2014 at 01:03 AM.

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Simplest way to read a joystick in VB6

    Does that work for a game controller too?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: Simplest way to read a joystick in VB6

    Quote Originally Posted by Nightwalker83 View Post
    Does that work for a game controller too?
    I think it would. Fundamentally there's nothing different between a joystick and a game-pad. It's just a different shaped controller.

    Update: I just tried it with the Microsoft Sidewinder Pro game-pad, and it works. The X and Y axis outputs from the joyGetPosEx API call even properly indicate the position of the controller's tilt sensor.
    Last edited by Ben321; Jun 17th, 2014 at 03:18 PM.

  4. #4
    New Member
    Join Date
    Jun 2014
    Posts
    2

    Re: Simplest way to read a joystick in VB6

    Hello ,
    Can I use joystick as buttun ??
    in other words , can i use joystick as mouse or keyboard ??

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Simplest way to read a joystick in VB6

    You can (it will involve calling mouse or keyboard API functions from the joystick reader), but that question, I think, should be posted out in the question and answer forums, not in the CodeBank forum.
    Last edited by passel; Jun 23rd, 2014 at 12:02 PM.

  6. #6
    New Member
    Join Date
    Mar 2015
    Posts
    2

    Re: Simplest way to read a joystick in VB6

    Quote Originally Posted by passel View Post
    You can (it will involve calling mouse or keyboard API functions from the joystick reader), but that question, I think, should be posted out in the question and answer forums, not in the CodeBank forum.
    hi

    i want build a simple project using joystick
    how can i find the code for each button of joystick ( like ascii code for keyboard buttons ) ??? for example, when i press "1" button on joystick , print 1 in the form


    do u have an idea for this ????? it's necessary for me ...... tnx

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Simplest way to read a joystick in VB6

    If you're using the code above, the buttons will be in the button fields.
    dwButtons As Long ' button states
    dwButtonNumber As Long ' current button number pressed

    I'm not completely sure about dwButtonNumber, but dwButtons should be 1 bit per button, if the bit is set the button is pressed.
    That means the interface above supports up to 32 buttons on a joystick device.
    You have to use a mask to test for the bits, to translate to a number.
    e.g.
    Code:
    Dim mask as Long
    If dwButtons And &H80000000 <> 0 Then
      Debug.Print "Button 32 pressed"
    End If
    mask = &H40000000
    For i = 31 to 1 step -1
      If dwButtons And mask <> 0  Then
        Debug.Print "Button ";i; "pressed"
        mask = mask \ 2
      End If
    Next
    The above code typed in here, not tried in VB6, but I believe it should work as an example.

    Perhaps dwButtonNumber does that masking decoding for you and reports the number 0 to 31, or 1 to 32, I don't know I would have to test, but the example code above returns 1 to 32 (assuming you had something with 32.

    Also, if it did, I don't know what it would report if more than one button was pressed at the same time (unless it just shows the last one).
    By testing a mask against dwButtons you can see multiple buttons down at the same time.

    I know of at least one joystick that used a kind of "bank" system, so you might have eight buttons, but a "bank" selection button or nob, that selected four positions, using bits from the "buttons" field to indicate the bank. That way you could have four modes that the eight buttons could be mapped to so essentially treat the eight buttons (8-bits) plus "bank" slection (2-bits) to have 32 states from 10 bits, without having to manually chord buttons.
    Last edited by passel; Mar 5th, 2015 at 03:51 PM.

  8. #8
    New Member
    Join Date
    Mar 2015
    Posts
    2

    Re: Simplest way to read a joystick in VB6

    Quote Originally Posted by passel View Post
    If you're using the code above, the buttons will be in the button fields.
    dwButtons As Long ' button states
    dwButtonNumber As Long ' current button number pressed

    I'm not completely sure about dwButtonNumber, but dwButtons should be 1 bit per button, if the bit is set the button is pressed.
    That means the interface above supports up to 32 buttons on a joystick device.
    You have to use a mask to test for the bits, to translate to a number.
    e.g.
    Code:
    Dim mask as Long
    If dwButtons And &H80000000 <> 0 Then
      Debug.Print "Button 32 pressed"
    End If
    mask = &H40000000
    For i = 31 to 1 step -1
      If dwButtons And mask <> 0  Then
        Debug.Print "Button ";i; "pressed"
        mask = mask \ 2
      End If
    Next
    The above code typed in here, not tried in VB6, but I believe it should work as an example.

    Perhaps dwButtonNumber does that masking decoding for you and reports the number 0 to 31, or 1 to 32, I don't know I would have to test, but the example code above returns 1 to 32 (assuming you had something with 32.

    Also, if it did, I don't know what it would report if more than one button was pressed at the same time (unless it just shows the last one).
    By testing a mask against dwButtons you can see multiple buttons down at the same time.

    I know of at least one joystick that used a kind of "bank" system, so you might have eight buttons, but a "bank" selection button or nob, that selected four positions, using bits from the "buttons" field to indicate the bank. That way you could have four modes that the eight buttons could be mapped to so essentially treat the eight buttons (8-bits) plus "bank" slection (2-bits) to have 32 states from 10 bits, without having to manually chord buttons.

    sorry, it isn't work for me in this code, dwButtonNumber give us the number of buttons that press
    dwButtons give a code for each button but ...... isn't work for navigation keys ...... i want that buttons !!!!!!!

    in my work, i write a programm for controling a robot ... i want that navigation buttons

    sorry for my bad EN lang

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