Buffered Mouse Data

To retrieve buffered data from the mouse, you must first set the buffer size (see Device Properties). The default size of the buffer is zero, so this step is essential. You then declare an array of DIDEVICEOBJECTDATA structures with the same number of elements as the buffer size.

After acquiring the device, you can examine and flush the buffer anytime by using the IDirectInputDevice::GetDeviceData method. (See Buffered and Immediate Data.)

Each element in the DIDEVICEOBJECTDATA array represents a change in state for a single object on the mouse. For instance, a typical mouse contains four objects or input sources: x-axis, y-axis, button 0 and button 1. If the user presses button 0 and moves the mouse diagonally, the array passed to IDirectInputDevice::GetDeviceData will have three elements filled in: an element for button 0 being pressed, an element for the change in the x-axis, and an element for the change in the y-axis.

You can determine which object an element in the array refers to by checking the dwOfs member of the DIDEVICEOBJECTDATA structure against the following values:

Each of these values is derived from the offset of the data for the object in a DIMOUSESTATE structure. For example, DIMOFS_BUTTON0 is equivalent to the offset of rgbButtons[0] in the DIMOUSESTATE structure. With the macros you can use simple comparisons to determine which device object is associated with an item in the buffer. For example:

DIDEVICEOBJECTDATA  *lpdidod; 
int                 n; 
. 
. 
. 
/* MouseBuffer is an array of DIDEVICEOBJECTDATA structures 
   that has been set by a call to GetDeviceData. 
   n is incremented in a loop that examines all filled elements 
   in the array. */ 
lpdidod = &MouseBuffer[n]; 
if (( (int) lpdidod->dwOfs == DIMOFS_BUTTON0) 
      && (lpdidod->dwData & 0x80)) 
{ 
 ;  // do something in response to left button press 
} 
 

The data for the change of state of the device object is located in the dwData member of the DIDEVICEOBJECTDATA structure. For axes, the coordinate value is returned in this member. For button objects, only the low byte of dwData is significant; the high bit of this byte is set if the button was pressed and clear if the button was released. In other words, the button was pressed if (dwData & 0x80) is nonzero.

For more information on the other members of the DIDEVICEOBJECTDATA structure, see Time Stamps and Sequence Numbers.