Share via


Step 4: Setting the Keyboard Behavior

Before your application can gain access to the keyboard, it must set the device's behavior using the IDirectInputDevice8::SetCooperativeLevel method, as shown in Tutorial 1 Samples. 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; 
} 

After setting the keyboard behavior, go to Step 5: Gaining Access to the Keyboard.