How to Read a USB Joystick in Visual Basic

Written by john mitchell Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Read a USB Joystick in Visual Basic
Portable USB joysticks make storing and retrieving important files simple. (usb stick memory image by dinostock from

Reading the data from a Universal Serial Bus (USB) joystick or thumb drive is possible with Microsoft Visual Basic (VB). Since USB devices are self-identifying and plug-and-play compliant, a computer system is able to easily recognise them. This makes writing a small piece of VB code to send a packet and retrieve data on the USB device easy. Sending a packet to the port is necessary, as it will detect the state of the port and send back any data found on the USB device. If there is no USB device plugged into the port, then an error message is displayed.

Skill level:


  1. 1

    Open a new VB file.

  2. 2

    Set the SendPacket function. Write this code at the top of your VB file:

    Function SendPacket(ByRef TxPacket As PacketStructure) As PacketStructure

    Dim lpResult As Long

    Dim RxPacket As PacketStructure

  3. 3

    Set the recipient and device model by inserting this code on the fifth line:

    TxPacket.Recipient = 8

    TxPacket.DeviceModel = 18

  4. 4

    Call the SendPacket function in the VB code. Write the below code starting on the ninth line:

    If 0 = DeviceIoControl(hDevice, CTL_CODE_SEND_PACKET, TxPacket,

    8+TxPacket.Length, RxPacket, 8, lpResult, 0) Then

    MsgBox "SendPacket function failed. Unable to retrieve USB data. "
    Exit Function

    End If

  5. 5

    Call back the data retrieved with the SendPacket function. Place this code at the bottom of your VB script:

    SendPacket = RxPacket

    End Function

  6. 6

    Insert your USB joystick into the computer's USB port.

  7. 7

    Compile and execute your VB script. Data is retrieved into the program's memory, which you can use for other programming solutions and needs.

Tips and warnings

  • The TxPacket.Recipient is always set to 8, and TxPacket.DeviceMode is set to 18.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.