DirectX SDK

Buffered Keyboard Data

To retrieve buffered data from the keyboard, you must first set the buffer size (see Device Properties). This step is essential because the default size of the buffer is 0.

[C++]

You must also declare an array of DIDEVICEOBJECTDATA structures. This array can have up to the same number of elements as the buffer size. You do not have to retrieve the entire contents of the buffer with a single call; if you want, you can have just one element in the array and retrieve events one at a time until the buffer is empty.

After acquiring the keyboard device, you can examine and flush the buffer at any time by using the IDirectInputDevice7::GetDeviceData method. (See Buffered and Immediate Data.)

Each element in the DIDEVICEOBJECTDATA array represents a change in state for a single key; that is, a press or release. Because DirectInput gets the data directly from the keyboard, any settings for character repeat in Control Panel are ignored. This means that a keystroke is counted only once, no matter how long the key is held down.

You can determine which key an element in the array refers to by checking the dwOfs member of the DIDEVICEOBJECTDATA structure against the DirectInput Keyboard Device Constants. (See also Interpreting Keyboard Data.)

The data for the change of state of the key is located in the dwData member of the DIDEVICEOBJECTDATA structure. Only the low byte of dwData is significant; the high bit of this byte is set if the key was pressed and clear if it was released. In other words, the key was pressed if (dwData & 0x80) is nonzero.

[Visual Basic]

You must also declare an array of DIDEVICEOBJECTDATA types. This array can have up to the same number of elements as the buffer size. You do not have to retrieve the entire contents of the buffer with a single call; if you want, you can have just one element in the array and retrieve events one at a time until the buffer is empty.

After acquiring the keyboard device, you can examine and flush the buffer at any time by using the DirectInputDevice.GetDeviceData method. (See Buffered and Immediate Data.)

Each element in the DIDEVICEOBJECTDATA array represents a change in state for a single key; that is, a press or release. Because DirectInput gets the data directly from the keyboard, any settings for character repeat in Control Panel are ignored. This means that a keystroke is counted only once, no matter how long the key is held down.

You can determine which key an element in the array refers to by checking the lOfs member of the DIDEVICEOBJECTDATA type against the constants in the CONST_DIKEYFLAGS enumeration. (See also Interpreting Keyboard Data.)

The data for the change of state of the key is located in the lData member of the DIDEVICEOBJECTDATA type. Only the low byte of lData is significant; the high bit of this byte is set if the key was pressed and clear if it was released. In other words, the key was pressed if (lData And &H80) is nonzero.