Microsoft DirectX 9.0

Step 2: Creating the DirectInput Keyboard Device

After creating the Microsoft?DirectInput?object, your application must create the keyboard object—the device—and retrieve a pointer to an IDirectInputDevice8 interface. The device performs most of the keyboard-related tasks, using the methods of the interface.

To do this your application must call the IDirectInput8::CreateDevice method, as shown in Sample Function 1: DI_Init. IDirectInput8::CreateDevice accepts three parameters.

The first parameter is the globally unique identifier (GUID) for the device being created. Because the system keyboard is used, your application should pass the GUID_SysKeyboard predefined global variable.

The second parameter is the address of a variable that is initialized with the interface pointer if the call succeeds.

The third parameter specifies the address of the controlling object's IUnknown interface for use in COM aggregation. Because most applications do not use aggregation, the value of this parameter is usually NULL.

The following example attempts to retrieve a pointer to an IDirectInputDevice8 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.
HRESULT               hr; 

hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL); 
if FAILED(hr) { 
    return FALSE; 

After creating the DirectInput keyboard device, go to Step 3: Setting the Keyboard Data Format.

© 2002 Microsoft Corporation. All rights reserved.