Microsoft DirectX 9.0

Step 4: Preparing for Buffered Input from the Mouse

The Scrawl application demonstrates how to use event notification to find out about mouse activity, and how to read buffered input from the mouse. Both of these techniques require some setup. You can perform these steps at any time after creating the mouse device and before acquiring it.

First, create an event and associate it with the mouse device. You are instructing Microsoft?DirectInput?to notify the mouse device object whenever a hardware interrupt indicates that new data is available.

This is how it is done in Scrawl, where g_hevtMouse is a global HANDLE.

g_hMouseEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

if (g_hMouseEvent == NULL) {
    return FALSE;

hr = g_pMouse->SetEventNotification(g_hMouseEvent);

if (FAILED(hr)) {
    return FALSE;

Now set the buffer size so that DirectInput can store any input data until you are ready to view it. Note that by default the buffer size is zero, so this step is essential if you want to use buffered data.

It is not necessary to used buffered data with event notification. If you prefer, you can retrieve immediate data when an event is signaled.

To set the buffer size, initialize a DIPROPDWORD structure with information about itself and about the property you want to set. Most of the values are boilerplate. The key value is the last one, dwData, which is initialized with the number of items you want the buffer to hold.


    // the header
    dipdw.diph.dwSize       = sizeof(DIPROPDWORD);
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
    dipdw.diph.dwObj        = 0;
    dipdw.diph.dwHow        = DIPH_DEVICE;
    // the data
    dipdw.dwData            = SAMPLE_BUFFER_SIZE;

You then pass the address of the header (the DIPROPHEADER structure within the DIPROPDWORD structure ), along with the identifier of the property you want to change, to the IDirectInputDevice8::SetProperty method, as follows:

hr = g_pMouse->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph);

if (FAILED(hr)) {
    return FALSE;

The setup is now complete, and you are ready to acquire the mouse and start collecting data.

After preparing your application for buffered input from the mouse, go to Step 5: Managing Access to the Mouse.

© 2002 Microsoft Corporation. All rights reserved.