Platform SDK: DirectX

Step 1: Create the DirectInput Object

[Visual Basic]

The information in this topic pertains only to applications written in C++. See DirectInput Visual Basic Tutorials.

[C++]

The first step in setting up the DirectInput system is to create a single DirectInput object as overall manager. This is done with a call to the DirectInputCreateEx function.

// HINSTANCE  hinst; // initialized earlier
HRESULT         hr; 
LPDIRECTINPUT7  g_lpdi; 
 
hr = DirectInputCreateEx(hinst, DIRECTINPUT_VERSION, 
        IID_IDirectInput7, (void**)&g_lpdi, NULL); 
if FAILED(hr) 
{ 
  // DirectInput not available; take appropriate action 
} 
 

The first parameter for DirectInputCreateEx is the instance handle to the application or DLL that is creating the object.

The second parameter tells the DirectInput object which version of the DirectInput system should be used. You can design your application to be compatible with earlier versions of DirectInput. For more information, see Designing for Previous Versions of DirectInput.

The third parameter determines which interface is returned. Most applications will obtain the most recent version, by passing IID_IDirectInput7.

The fourth parameter is the address of a variable that will be initialized with a valid interface pointer if the call succeeds.

The last parameter specifies the address of the controlling object's IUnknown interface for use in COM aggregation. Most applications will not be using aggregation and so will pass NULL.

Next: Step 2: Create the DirectInput Keyboard Device