Simplest way to read a joystick in VB6-VBForums
Results 1 to 5 of 5

Thread: Simplest way to read a joystick in VB6

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2008
    Posts
    471

    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
    Web developer Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    12,709

    Re: Simplest way to read a joystick in VB6

    Does that work for a game controller too?
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    Please consider giving me some rep points if I help you a lot.
    Links to my code examples can now be found on my website: My websites
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2008
    Posts
    471

    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
    Fanatic Member
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    820

    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.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.