8.3. Types of Inputs 343
8.3 Types of Inputs
Although human interface devices for games vary widely in terms of form
factor and layout, most of the inputs they provide fall into one of a small num-
ber of categories. We’ll investigate each category in depth below.
8.3.1. Digital Buttons
Almost every HID has at least a few digital butt ons. These are butt ons that can
only be in one of two states: pressed and not pressed. Game programmers oft en
refer to a pressed butt on as being down and a non-pressed butt on as being up.
Electrical engineers speak of a circuit containing a switch as being closed
(meaning electricity is fl owing through the circuit) or open (no electricity is
fl owing—the circuit has infi nite resistance). Whether closed corresponds to
pressed or not pressed depends on the hardware. If the switch is normally open,
then when it is not pressed (up), the circuit is open, and when it is pressed
(down), the circuit is closed. If the switch is normally closed, the reverse is true—
the act of pressing the butt on opens the circuit.
In soft ware, the state of a digital butt on (pressed or not pressed) is usually
represented by a single bit. It’s common for 0 to represent not pressed (up)
and 1 to represent pressed (down). But again, depending on the nature of the
circuitry, and the decisions made by the programmers who wrote the device
driver, the sense of these values might be reversed.
It is quite common for the states of all of the butt ons on a device to be
packed into a single unsigned integer value. For example, in Microsoft ’s
XInput API, the state of the Xbox 360 joypad is returned in a struct called
XINPUT_GAMEPAD, shown below.
typedef struct _XINPUT_GAMEPAD {
WORD wButtons;
BYTE bLeftTrigger;
BYTE bRightTrigger;
SHORT sThumbLX;
SHORT sThumbLY;
SHORT sThumbRX;
SHORT sThumbRY;
} XINPUT_GAMEPAD;
This struct contains a 16-bit unsigned integer (WORD) variable named
wButtons that holds the state of all butt ons. The following masks defi ne