home | login | register | DMCA | contacts | help | donate |      

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z


my bookshelf | genres | recommend | rating of books | rating of authors | reviews | new | | collections | | | add
fantasy
space fantasy
fantasy is horrors
heroic
prose
  military
  child
  russian
detective
  action
  child
  ironical
  historical
  political
western
adventure
adventure (child)
child's stories
love
religion
antique
Scientific literature
biography
business
home pets
animals
art
history
computers
linguistics
mathematics
religion
home_garden
sport
technique
publicism
philosophy
chemistry
close

Loading...


Setting up the keyboard

Once we have created DirectInput interface pointer, we are ready to setup the keyboard. Shown below is the code required to setup the keyboard, take a look at it and I'll explain it below.

//KEYBOARD =======================================================================

//Create the keyboard device object

if (FAILED(m_pDirectInput->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, NULL))) {

CleanUpDirectInput();

LogError("

  • Unable to create DirectInput keyboard device interface.");

    return false;

    } else {

    LogInfo("

  • DirectInput keyboard device interface created OK.");

    }

    //Set the data format for the keyboard

    if (FAILED(m_pKeyboard->SetDataFormat(&c_dfDIKeyboard))) {

    CleanUpDirectInput();

    LogError("

  • Unable to set the keyboard data format.");

    return false;

    } else {

    LogInfo("

  • Set the keyboard data format OK.");

    }

    //Set the cooperative level for the keyboard

    if (FAILED(m_pKeyboard->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))) {

    CleanUpDirectInput();

    LogError("

  • Unable to set the keyboard cooperative level.");

    return false;

    } else {

    LogInfo("

  • Set the keyboard cooperative level OK.");

    }

    //Acquire the keyboard

    if (m_pKeyboard) {

    m_pKeyboard->Acquire();

    }

    First of all, we need to create a keyboard device. We do this by calling the CreateDevice method of DirectInput. The first parameter is the GUID (Globally Unique Identifier) of the device to create, in our case the system keyboard device, so we simply pass in the GUID_SysKeyboard predefined GUID. The next parameter will receive a pointer to the keyboard device, we have defined this variable as a member of CGame. The last parameter is always NULL, take a look in the SDK for further details.

    The next thing to do is set the data format for the keyboard device. To do this we simply pass in the predefined keyboard format c_dfDIKeyboard.

    Then, we need to set how our input device (the keyboard) will cooperate with our application and Windows. We will need to specify the cooperation level by selecting "foreground" or "background" and "exclusive" or "nonexclusive". To do this, we need to pass in the relevant flags (DISCL_FOREGROUND, DISCL_BACKGROUND, DISCL_EXCLUSIVE and DISCL_NONEXCLUSIVE) to the SetCooperativeLevel method.

    DISCL_FOREGROUND: Input device is available when the application is in the foreground (has focus).

    DISCL_BACKGROUND: Input device is available at all times, foreground and background.

    DISCL_EXCLUSIVE: No other instance of the device can obtain exclusive access to the device while it is acquired.

    DISCL_NONEXCLUSIVE: Access to the device does not interfere with other applications that are accessing the same device.

    The final thing to do is to acquire the keyboard, this means that we can now get access to the device's data. We do this by calling the Acquire method of the device.


  • Initialising DirectInput | DirectX 8 Programming Tutorial | Setting up the mouse







    Loading...