Home | Gaming | Programming | Play Online | Contact | Keyword Query
Games++ Games & Game Programming

Games++ Home
Games++ Gaming
Games++ Programming
Beta Testing Games
Free Online Games
Hints & Cheats

E-mail This Page
Add to Favorites


Web Games++

Cheat Codes
Trickster Wiki
Game Ratings
Gameboy Cheats
PlayStation Cheats
BlackBerry Games
Photoshop Tutorials
Illustrator Tutorials
ImageReady Tutorials


DirectInput Initialization

DirectX Programming

Anthony Wlodarski

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;
   // 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.

   // 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?


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.

char buffer[256];
   // 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 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)
Copyright © 1998-2007, Games++ All rights reserved. | Privacy Policy