Topic : DirectInput Initialization
Author : Anthony Wlodarski
Page : 1

STEP 1 - Creating the Direct Input Object


We must create the DirectInput Object with the DirectInputCreateEx(); function. It takes five parameters. The first is a handle to the instance of the application, the second parameter should be filled in with the DIRECTINPUT_VERSION flag, so as to use the latest version of DirectInput. The third parameter is the IID_IDirectInput7 interface. The fourth parameter is the address of the LPDIRECTINPUT7 object and the last parameter is always NULL. So all my words jumbled together may be represented by the following code.

// Initialized in WinMain();

HINSTANCE main_instance;
LPDIRECTINPUT7 lpDI;

if(FAILED(DirectInputCreate(main_instance,
DIRECTINPUT_VERSION,IID_IDirectInput7,&lpDI,NULL)))
{
   // ERROR
}


STEP 2 - Creating the Keyboard Device Object


Now we must creat a device object for the keyboard using CreateDeviceEx(); which takes four parameters. The first paramater is a GUID for the keyboard which is GUID_SysKeyboard, the second parameter is the interface IID_DirectInputDevice7, the third parameter is the address of the LPDIRECTDRAWDEVICE7 object. The last parameter is NULL.

LPDIRECTINPUTDEVICE7 lpDIkeyboard;

if(FAILED(lpDI->CreateDeviceEx(GUID_SysKeyboard,
IID_IDirectInputDevice7,&lpDIkeyboard,NULL)))
{
   // ERROR
}


STEP 3 - Setting the Data Format


The next thing is to set the data format for us to read from with the global variable "c_dfDIKeyboard." The member function of the LPDIRECTDRAWDEVICE7 object used for this task is SetDataFormat(); and it only takes one paramater, can you guess what that is?

if(FAILED(lpDIkeyboard->SetDataFormat(&c_dfDIkeyboard)))
{
   //ERROR
}


STEP 4 - Playing Nice With Windows


Now we must set the cooperative level with windows; if you are familiar with other Direct X interfaces then you know how this may look. We set the cooperation by each individual object not by the LPDIRECTINPUT7 object. So this function is a member function of the LPDIRECTINPUTDEVICE7 object. The function SetCooperativeLevel(); takes two parameters, the first is a handle to the window of the application and the second is a set of flags which I will explain later.

// Initialized in WinMain();
HWND main_window;

if(FAILED(lpDIkeyboard->SetCooperativeLevel(main_window,
DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)))
{
   // ERROR
}


The DISCL_NONEXCLUSIVE flag does the following things: It will keep Windows in control if the other applications need it. This is good because if you take Windows too much out of the program you may not be able to get back through the keyboard. The DISCL_BACKGROUND allows the application to use the DirectInput device if the other application is idling in the background.

STEP 5 - Acquring the Keyboard


This is the easiest step because it is a call to the member function Acquire(); to get the keyboard control.

lpDIkeyboard->Acquire();

STEP 6 Getting Input and Playing With It
This is necesarry if you wish to get input from the keyboard, you must check what it is doing in your game loop and act accordingly. The member function GetDeviceState(similar to the Windows API) takes two paramaters. The first is the size of a character array, which is used to keep the 256 BYTE data. So therefore our character array should be of size 256.

char buffer[256];

if(FAILED(lpDIkeyboard->GetDeviceState(sizeof(buffer),&buffer)))
{
   // ERROR
}


Now Microsoft has been kind enough to give us a little macro to use and fits perfectly for its use

#define KEYDOWN(name,key) (name[key] & 0x80)
if(KEYDOWN(buffer,DIK_ESCAPE))
   PostQuitMessage(0);


STEP 7 - SHUTTING DOWN!(VERY IMPORTANT)


If you do not shut down correctly you will not have a pleasant experience afterwards. So this is what I use:

if(lpDI != NULL)
{
   if(lpDIkeyboard != NULL)
   {
      lpDIkeyboard->Unacquire();
      lpDIkeyboard->Release();
   }
   lpDI-Release();
}



Page : 1