Microsoft DirectX 8.1 (Visual Basic)

Step 4: Retrieving Immediate Data from the Joystick

Now that the necessary properties have been set, the device can be acquired and data can be collected from the joystick. In the Joystick Sample, the device is polled in a loop after its properties have been set. This uses the DirectInputDevice8.Poll method. In a real-world multimedia application, polling would take place in the main game loop or rendering loop.

diDev.Acquire
 
While DoEvents
    diDev.Poll
Wend

The sample application does not actually retrieve immediate data each time the device is polled. To avoid unnecessary screen updates, it relies on notification, and the call to Poll is only to ensure that notifications are issued by devices that do not generate interrupts. Polling is not necessary for some game devices (such as HIDs), but it is as efficient to make a redundant call to DirectInputDevice8.Poll as to check a flag for the device before calling Poll.

Notifications are handled, as usual, in the implementation of the DirectXEvent8.DXCallback method. Each time this procedure is called, the application knows that a joystick event has occurred. At this point the application could call DirectInputDevice8.GetDeviceData to retrieve any pending axis changes or button events. However, with a game controller it is more common to retrieve the state of the entire device. Therefore, the sample uses DirectInputDevice8.GetDeviceStateJoystick, called from within the callback function.

Dim js As DIJOYSTATE
 
diDev.GetDeviceStateJoystick js

The values in the DIJOYSTATE type contain the state of all the device objects on the joystick. Because Step 2: Getting Joystick Capabilities checked which buttons, axes, and point-of-view controllers are actually present, you know which members in the type to ignore.