Microsoft DirectX 8.1 (Visual Basic)

Step 1: Enumerating Devices and Creating the Joystick

Because there is no such thing as a system joystick in the same sense that there can be a system keyboard or mouse, in order to create a DirectInputDevice8 object for a joystick you first need to obtain an instance GUID, or globally unique identifier. Generally, this is done by enumerating the available joysticks, presenting the user with a choice, and then obtaining the information for the selected device.

As with all devices, the DirectInput system must first be created.

Dim dx As New DirectX8
Dim di As DirectInput8

Set di = dx.DirectInputCreate()

The next step is enumeration of devices using the DirectInput8.GetDIDevices method. As a parameter to this method, flags are passed indicating that the application should enumerate only game controller devices that are physically attached to the system. The results of this enumeration are stored in an instance of the DirectInputEnumDevices8 class.

Dim diDev As DirectInputDevice8
Dim diDevEnum As DirectInputEnumDevices8

Set diDevEnum = di.GetDIDevices( _
            DI8DEVCLASS_GAMECTRL, DIEDFL_ATTACHEDONLY)

Once the enumeration is complete, assuming at least one joystick was found, that information can be manipulated. In the case of the Joystick Sample, the devices are added to a list box from which the user can make a choice. The DirectInputEnumDevices8.GetCount method specifies how many items are contained in the enumeration.

'Add attached joysticks to the list box
Dim i As Integer
For i = 1 To diDevEnum.GetCount
    Call lstJoySticks.AddItem( _
        diDevEnum.GetItem(i).GetInstanceName)
Next

GetInstanceName returns a friendly name for the device.

At this point, the Joystick Sample initializes an event handle to associate with the device. This is used at the end of Step 2: Getting Joystick Capabilities.

Dim EventHandle as Long
EventHandle = dx.CreateEvent(Me)

When the user selects a joystick from the list, the device is created and initialized in the lstJoySticks_Click procedure. Because there are no standard GUIDs to pass to DirectInput8.CreateDevice, as there are for the system keyboard and system mouse, the GUID of the device instance must be extracted by using the DirectInputDeviceInstance8.GetGuidInstance method. Note that as with all enumerated collections in Microsoft DirectX® for Microsoft Visual Basic®, the device enumeration is 1-based. Therefore, the index is one greater than the index of the selected item in the list box. The list box must also be unsorted.

Set diDev = di.CreateDevice(diDevEnum.GetItem( _
        lstJoySticks.ListIndex + 1).GetGuidInstance)

The joystick must also be initialized for cooperative level and format. In this case, the DIFORMAT_JOYSTICK format is used. Another format, DIFORMAT_JOYSTICK2 is also available for joysticks with a larger assortment of controls.

diDev.SetCommonDataFormat DIFORMAT_JOYSTICK
diDev.SetCooperativeLevel Me.hWnd, _
        DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE

You must set the data format before attempting to enumerate objects on the device or manipulate its properties.

Once the joystick is in place, its capabilities are explored in Step 2: Getting Joystick Capabilities