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();
           }

    }

You might also like...

Comments

About the author

David Nishimoto United States

NishiSoft provides Part I of the Information Technology Project collaboration. Sign up and list your IT project tasks, assign task too friends, and get percent complete task.

Part will ...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Never trust a programmer in a suit.” - Anonymous