Step 2: Creating the DirectInput Keyboard Device
After creating the DirectInput object, your application must create the keyboard object-the device-and retrieve a pointer to an IDirectInputDevice interface. The device will perform most of the keyboard-related tasks, using the methods of the interface.
To do this your application must call the IDirectInput::CreateDevice method, as shown in Sample Function 1: DI_Init. CreateDevice accepts three parameters.
The first parameter is the GUID for the device being created. Since the system keyboard will be used, your application should pass the GUID_SysKeyboard value.
The second parameter is the address of a variable that will be initialized with a valid IDirectInputDevice interface pointer if the call succeeds.
The third parameter specifies the address of the controlling object's IUnknown interface for use in COM aggregation. Your application will likely not use aggregation, in which case the parameter is NULL.
The following example attempts to retrieve a pointer to an IDirectInputDevice interface. If this fails, it calls the DI_Term application-defined sample function to deallocate existing DirectInput objects, if any.
Note In all the examples, g_lpdi is the initialized pointer to the DirectInput object. The method calls are in the C++ form.
HRESULT hr;
LPDIRECTINPUTDEVICE g_lpDIDEVICE
hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL);
if FAILED(hr)
{
DI_Term();
return FALSE;
}