1. Direct Input Architecture
- Enumerate system devices by ID using
DirectInput8Create
- Call
CreateDevice
using the ID for the device to get a list of device components - Use
g_pKeyboard->SetDataFormat
to determine what key has been pressed org_pMouse->SetDataFormat
to determine which mouse keys have been pressed and the mouse position. - Call
GetDeviceState
to determine the current status of a device
2. Obtaining the Device ID for the KeyBoard
- Create a Dialog resource
IDD_DIRECTINPUT
- 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 - Use the Class Wizard to create a Class called
CDirectInput1View
associated withIDD_DIRECTINPUT
.
Make sure you select CFormView as the parent class type. - Create a Message Map for
IDC_CREATEDEVICE
callOnCreateDevice
, so when the user press the Create Device Button theOnCreatedevice
method is invoked. - 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; - Replace the
#include "directinputview.h"
statement with#include "directinput1view.h"
in thedirectinputview.cpp
source.
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_DIRECTTYPE,
RUNTIME_CLASS(CDirectinputDoc),
RUNTIME_CLASS(CChildFrame),
RUNTIME_CLASS(CDirectInput1View));
AddDocTemplate(pDocTemplate); - 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