DirectX SDK

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 0, so this step is essential.

[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 device, you can examine and flush events in the buffer at any time by using the IDirectInputDevice7::GetDeviceData method. (See Buffered and Immediate Data.) On return, each element in the DIDEVICEOBJECTDATA array represents a change in state for a single object on the mouse. For example, if the user presses button 0 and moves the mouse diagonally, the array passed to GetDeviceData (if it has at least three elements, and pdwInOut is at least 3) has 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—and the value of pdwInOut is set to 3.

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

Each of these values is derived from the offset of the data for the object in a DIMOUSESTATE or DIMOUSESTATE2 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.

[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 device, you can examine and flush the buffer at any time by using the DirectInputDevice.GetDeviceData method. (See Buffered and Immediate Data.) On return, each element in the DIDEVICEOBJECTDATA array represents a change in state for a single object on the mouse. For instance, if the user presses button 0 and moves the mouse diagonally, the array passed to GetDeviceData (if it has at least three elements) has 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—and the return value of the method is 3.

You can determine which object an element in the array refers to by checking the lOfs member of the DIDEVICEOBJECTDATA type against the constants in the CONST_DIMOUSEOFS enumeration. Each of these values is derived from the offset of the data for the object in a DIMOUSESTATE type. For example, DIMOFS_BUTTON0 is equivalent to the offset of buttons(0) in the DIMOUSESTATE type.

The data for the change of state of the device object is located in the lData member of the DIDEVICEOBJECTDATA type. For axes, the coordinate value is returned in this member. For button objects, only the low byte of lData 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 (lData & 0x80) is nonzero.

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

The following code example retrieves the entire contents of the buffer (which contains BufferSize elements) and responds to various events:

' objDIDev is a DirectInputDevice object.
Dim diDeviceData(1 To BufferSize) As DIDEVICEOBJECTDATA
Dim NumEvents As Integer
Dim i As Integer
 
NumEvents = objDIDev.GetDeviceData(diDeviceData, 0)
For i = 1 To NumEvents 
    Select Case diDeviceData(i).lOfs
        Case DIMOFS_X
           ' Respond to x-axis movement.
 
         Case DIMOFS_Y
           ' Respond to y-axis movement.
 
         Case DIMOFS_BUTTON0
           If diDeviceData(i).lData And &H80 Then
             ' Respond to left button pressed.
           Else
             ' Respond to left button released.
           End If
 
    End Select
Next i