Step 3: Setting the Keyboard Data Format

After retrieving an IDirectInputDevice pointer, your application must set the device's data format, as shown in Sample Function 1: DI_Init. For keyboards, this is a very simple task. Call the IDirectInputDevice::SetDataFormat method, specifying the data format provided for your convenience by DirectInput in the c_dfDIKeyboard global variable.

The following example attempts to set the data format. If this fails, it calls the DI_Term sample function to deallocate existing DirectInput objects, if any.

hr = g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard); 
 
if FAILED(hr){ 
    DI_Term(); 
    return FALSE; 
}