Library tutorials & articles

Direct Input 8

Getting Started

1. Direct Input Architecture

  1. Enumerate system devices by ID using DirectInput8Create
  2. Call CreateDevice using the ID for the device to get a list of device components
  3. Use g_pKeyboard->SetDataFormat to determine what key has been pressed or g_pMouse->SetDataFormat to determine which mouse keys have been pressed and the mouse position.
  4. Call GetDeviceState to determine the current status of a device

2. Obtaining the Device ID for the KeyBoard

  1. Create a Dialog resource IDD_DIRECTINPUT
  2. Include the following controls on the dialog:
    IDC_CREATEDEVICE m_create_device(CButton)
    IDC_DATA (CString) m_data
    IDC_EXCLUSIVE (int,CButton) m_exclusive & m_exclusive_control
    IDC_FOREGROUND (int,CButton) m_foreground & m_foreground_control
    IDC_FREE_DEVICE(CButton) m_free_device_control
    IDC_IMMEDIATE (int,CButton) m_immediate and m_immediate_control
    IDC_WINDOWSKEY (BOOL, CButton) m_windowskey and m_windowskey_control
  3. Use the Class Wizard to create a Class called CDirectInput1View associated with IDD_DIRECTINPUT.
    Make sure you select CFormView as the parent class type.
  4. Create a Message Map for IDC_CREATEDEVICE call OnCreateDevice, so when the user press the Create Device Button the OnCreatedevice method is invoked.
  5. Add the following defines to the CDirectInput1View.h file.

    #define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
    #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }

    #define SAMPLE_BUFFER_SIZE 8 // arbitrary number of buffer elements

    LPDIRECTINPUT8 g_pDI = NULL;
    LPDIRECTINPUTDEVICE8 g_pKeyboard = NULL;

  6. Replace the #include "directinputview.h" statement with #include "directinput1view.h" in the directinputview.cpp source.

    CMultiDocTemplate* pDocTemplate;
    pDocTemplate = new CMultiDocTemplate(
          IDR_DIRECTTYPE,
          RUNTIME_CLASS(CDirectinputDoc),
          RUNTIME_CLASS(CChildFrame),
          RUNTIME_CLASS(CDirectInput1View));
    AddDocTemplate(pDocTemplate);
  7. Create a Message Map for IDC_CREATEDEVICE using the wizard. This code will be inserted into the directinput1view.cpp source code.

    void CDirectInput1View::OnCreatedevice()
    {
       
       /*
       The CreateKeyBoardDevice is describe in the method below.
       The AfxGetMainWnd() macro returns the MainWnd object.
       MFC hierarchy is composed first of an application
       then MainFrame which has a main window, and last
       the view.  Returning m_hWnd gives the current
       view window handle so using AfxGetMainWnd is
       used instead.
       */

       

          if( NULL == g_pKeyboard )
           {
              if( FAILED( CreateKeyBoardDevice(AfxGetMainWnd()->m_hWnd) ) )
               {
                   MessageBox( _T("CreateDevice() failed. ")
                                _T("The sample will now exit."),
                                     _T("Keyboard"), MB_ICONERROR | MB_OK );
                   FreeDirectInput();
               }
           }
           else
           {
               FreeDirectInput();
           }

    }

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Direct Input 8.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related discussion

Events coming up

  • Dec 6

    Developing AJAX Web Applications with Castle Monorail

    London, United Kingdom

    Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!