Platform SDK: DirectX

Step 2: Create the DirectInput Keyboard Device

[Visual Basic]

The information in this topic pertains only to applications written in C++. See DirectInput Visual Basic Tutorials.

[C++]

After creating the DirectInput object, your application must create the keyboard object—the device—and retrieve a pointer to an IDirectInputDevice7 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 IDirectInput7::CreateDeviceEx method, as shown in Sample Function 1: DI_Init. CreateDeviceEx accepts four 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 predefined global variable.

The second parameter is the GUID for the desired interface. Most applications will obtain the most recent version, by passing IID_IDirectInputDevice7.

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

The fourth 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 IDirectInputDevice7 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; 
LPDIRECTINPUTDEVICE7  g_lpDIDEVICE 
 
hr = g_lpDI->CreateDeviceEx(GUID_SysKeyboard, IID_IDirectInputDevice7,
        (void**)&g_lpDIDevice, NULL); 
if FAILED(hr) 
{ 
    DI_Term(); 
    return FALSE; 
} 

Next: Step 3: Set the Keyboard Data Format