Capturing Joystick Messages to a Window Function

You can capture joystick input to a window function; your application then receives joystick messages at specified intervals or when the user manipulates the joystick. The messages are described in “Processing Joystick Messages,” following this section.

Only one application can capture joystick messages from a given joystick. Capturing joystick messages does not, however, prevent your application (or other applications) from polling the joystick using joyGetPos. If joyGetPos is called while joystick input is captured, joystick events occurring close to the time of the joyGetPos call might not be accurately reported to the capture window.

Summary: Capturing Joystick Input

The joySetCapture function captures joystick input to a window function you specify. To release the joystick, call the joyReleaseCapture function. The joySetCapture function has the following syntax:

WORD joySetCapture(hWnd, wID, wPeriod, bChanged)

Specify the handle of the window to receive the messages in the hWnd parameter. For wID, specify which joystick to capture; use the constants JOYSTICKID1 or JOYSTICKID2. The wPeriod parameter specifies the frequency, in milliseconds, of the joystick messages, and the bChanged parameter specifies whether messages are to be sent only when the stick position or button states change. The joystick messages are described in the next section.

To capture messages from two joysticks attached to the system, you must call joySetCapture twice, once for each joystick. Your window then receives separate messages for each device.

You cannot capture an unplugged joystick. The joySetCapture function returns zero if successful; it returns JOYERR_UNPLUGGED if the specified device is unplugged.

Note:

The joystick services set up a Windows timer event with each call to joySetCapture.

Summary: Specifying the Resolution and Threshold

Assign the wPeriod parameter a value that falls within the minimum and maximum resolution range for the joystick. To determine the minimum and maximum resolution of the joystick, call the joyGetDevCaps function, which fills the wPeriodMin and wPeriodMax fields in the JOYCAPS structure.

If the wPeriod value is outside the range of valid resolution values for the joystick, the joystick services use the minimum or maximum resolution value, whichever is closer to the wPeriod value.

The bChanged parameter controls when the window receives joystick movement messages. If bChanged is set to FALSE, these messages occur approximately every wPeriod milliseconds, regardless of whether the position has changed since the last time the joystick was polled. If bChanged is set to TRUE, messages are sent when the position of a joystick axis changes by a value greater than the movement threshold of the device. To change the movement threshold, use the joySetThreshold function, discussed in “Setting the Movement Threshold,” later in this chapter.