Jump to content
  • Advertisement
  • 02/21/01 11:03 AM
    Sign in to follow this  

    DirectInput Initialization

    Graphics and GPU Programming

    Myopic Rhino
    [size="5"]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

    [size="5"]STEP 2 - Creating the Keyboard Device Object

    Now we must create a device object for the keyboard using CreateDeviceEx(); which takes four parameters. The first parameter 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

    [size="5"]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 parameter, can you guess what that is?


    [size="5"]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.

    [size="5"]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.


    [size="5"]STEP 6 Getting Input and Playing With It

    This is necessary 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 parameters. 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];

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

      Report Article
    Sign in to follow this  

    User Feedback

    There are no comments to display.

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!