Setting the Cooperative Level

The next step is to set the cooperative level, which determines how much of the keyboard DirectInput will give your program by way of priority. To set the cooperative level, you call the SetCooperativeLevel function. The first parameter is the window handle. The second parameter is the interesting one—it specifies the priority that your program will have over the keyboard or mouse. The most common values to pass when working with the keyboard are DISCL_EXCLUSIVE and DISCL_FOREGROUND (in either full-screen or windowed mode). When you gain exclusive use of the keyboard, DirectInput will still allow key combinations such as Alt+Tab and Ctrl+Alt+Delete because those key combinations are detected at a lower level within Windows. So, here is how you might call the function:

HRESULT result = dikeyboard->SetCooperativeLevel( hwnd,
    DISCL_NONEXCLUSIVE | DISCL_FOREGROUND );

There is a valid argument to be made for acquiring the keyboard and mouse in nonexclusive mode in order to support screen captures and minimizing the window, among other reasons. While developing a game, I like to work in nonexclusive mode so I can do these things without having to restart a program using DirectInput. If the program window loses focus, then DirectInput loses the devices—requiring a manual re-acquire on our part. That being the case, using exclusive mode does not really cause any problems. I recommend nonexclusive for the keyboard and exclusive for the mouse. Give it a try, in both windowed and full-screen modes, and determine which level of control you prefer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset