DirectX SDK |
The information in this topic pertains only to applications written in C++. See DirectInput Visual Basic Tutorials.
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 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 DirectInput to notify the mouse device object whenever a hardware interrupt indicates that new data is available.
This is how it's done in Scrawl, where g_hevtMouse is a global HANDLE.
g_hevtMouse = CreateEvent(0, 0, 0, 0); if (g_hevtMouse == NULL) { return FALSE; } hr = g_pMouse->SetEventNotification(g_hevtMouse); if (FAILED(hr)) { return FALSE; }
Now you need to set the buffer size so that DirectInput can store any input data until you're ready to look at it. Remember, by default the buffer size is zero, so this step is essential if you want to use buffered data.
It's 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 you need to initialize a DIPROPDWORD structure with information about itself and about the property you wish 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.
#define DINPUT_BUFFERSIZE 16 DIPROPDWORD dipdw = { // the header { sizeof(DIPROPDWORD), // diph.dwSize sizeof(DIPROPHEADER), // diph.dwHeaderSize 0, // diph.dwObj DIPH_DEVICE, // diph.dwHow }, // the data DINPUT_BUFFERSIZE, // dwData };
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 IDirectInputDevice7::SetProperty method, as follows:
hr = g_pMouse->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph); if (FAILED(hr)) { return FALSE; }
The setup is now complete, and you're ready to acquire the mouse and start collecting data.