Platform SDK: DirectX

Step 4: Set the Keyboard Behavior

[Visual Basic]

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

[C++]

Before your application can gain access to the keyboard, it must set the device's behavior using the IDirectInputDevice7::SetCooperativeLevel method, as shown in Sample Function 1: DI_Init. This method accepts the handle to the window to be associated with the device and a combination of flags that determine the cooperative level. In this case the application is requesting foreground, nonexclusive access.

The following example attempts to set the device's cooperative level. If this fails, it calls the DI_Term application-defined sample function to deallocate existing DirectInput objects, if any.

// Set the cooperative level 
hr = g_lpDIDevice->SetCooperativeLevel(g_hwndMain, 
                   DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 
 
if FAILED(hr){ 
    DI_Term(); 
    return FALSE; 
} 

Next: Step 5: Gain Access to the Keyboard