Книга: DirectX 8 Programming Tutorial

Setting up the keyboard

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("<li>Unable to create DirectInput keyboard device interface.");
 return false;
} else {
 LogInfo("<li>DirectInput keyboard device interface created OK.");
}
//Set the data format for the keyboard
if (FAILED(m_pKeyboard->SetDataFormat(&c_dfDIKeyboard))) {
 CleanUpDirectInput();
 LogError("<li>Unable to set the keyboard data format.");
 return false;
} else {
 LogInfo("<li>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("<li>Unable to set the keyboard cooperative level.");
 return false;
} else {
 LogInfo("<li>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.

Оглавление книги


Генерация: 0.289. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз