Step 1: Creating the DirectInput Object
// HINSTANCE g_hinst; // initialized earlier
HRESULT hr;
LPDIRECTINPUT8 g_lpDI;
hr = DirectInput8Create(g_hinst, DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&g_lpDI, NULL);
if FAILED(hr)
{
// DirectInput not available; take appropriate action
}
Step 2: Creating the DirectInput Keyboard Device
HRESULT hr; LPDIRECTINPUTDEVICE8 g_lpDIDevice hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL); if FAILED(hr) { DI_Term(); return FALSE; }
Step 3: Setting the Keyboard Data Format
hr = g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard); if FAILED(hr) { DI_Term(); return FALSE; }
Step 4: Setting the Keyboard Behavior
hr = g_lpDIDevice->SetCooperativeLevel(g_hwndMain, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); if FAILED(hr) { DI_Term(); return FALSE; }
Step 5: Gaining Access to the Keyboard
if (g_lpDIDevice) g_lpDIDevice->Acquire();
Step 6: Retrieving Data from the Keyboard
void WINAPI ProcessKBInput() { #define KEYDOWN(name, key) (name[key] & 0x80) char buffer[256]; HRESULT hr; hr = g_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer); if FAILED(hr) { // If it failed, the device has probably been lost. // Check for (hr == DIERR_INPUTLOST) // and attempt to reacquire it here. return; } // Turn the spaceship right or left if (KEYDOWN(buffer, DIK_RIGHT)); // Turn right. else if(KEYDOWN(buffer, DIK_LEFT)); // Turn left. // Thrust or stop the spaceship if (KEYDOWN(buffer, DIK_UP)) ; // Move the spaceship forward. else if (KEYDOWN(buffer, DIK_DOWN)); // Stop the spaceship. }Step 7: Closing Down the DirectInput System
When an application is about to close, it should destroy all Microsoft® DirectInput® objects. This is a three-step process.
- Release access to all DirectInput devices (IDirectInputDevice8::Unacquire).
- Release all DirectInput devices (IDirectInputDevice8::Release).
- Release the DirectInput object (IDirectInput8::Release).
Sample Function 1: DI_Init
/* The following variables are presumed to be initialized: HINSTANCE g_hinst; // application instance HWND g_hwndMain; // application window */ LPDIRECTINPUT8 g_lpDI; LPDIRECTINPUTDEVICE8 g_lpDIDevice; BOOL WINAPI DI_Init() { HRESULT hr; // Create the DirectInput object. hr = DirectInput8Create(g_hinst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&g_lpDI, NULL); if FAILED(hr) return FALSE; // Retrieve a pointer to an IDirectInputDevice8 interface hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL); if FAILED(hr) { DI_Term(); return FALSE; } // Now that you have an IDirectInputDevice8 interface, get // it ready to use. // Set the data format using the predefined keyboard data // format provided by the DirectInput object for keyboards. hr = g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard); if FAILED(hr) { DI_Term(); return FALSE; } // Set the cooperative level hr = g_lpDIDevice->SetCooperativeLevel(g_hwndMain, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); if FAILED(hr) { DI_Term(); return FALSE; } // Get access to the input device. hr = g_lpDIDevice->Acquire(); if FAILED(hr) { DI_Term(); return FALSE; } return TRUE; }
Sample Function 2: DI_Term
/* The following variables are presumed initialized: LPDIRECTINPUT8 g_lpDI; LPDIRECTINPUTDEVICE8 g_lpDIDevice; */ void WINAPI DI_Term() { if (g_lpDI) { if (g_lpDIDevice) { // Always unacquire device before calling Release(). g_lpDIDevice->Unacquire(); g_lpDIDevice->Release(); g_lpDIDevice = NULL; } g_lpDI->Release(); g_lpDI = NULL; } }