Library tutorials & articles
Direct Input 8
By David Nishimoto, published on 25 Aug 2002
Getting Started
1. Direct Input Architecture
- Enumerate system devices by ID using
DirectInput8Create - Call
CreateDeviceusing the ID for the device to get a list of device components - Use
g_pKeyboard->SetDataFormatto determine what key has been pressed org_pMouse->SetDataFormatto determine which mouse keys have been pressed and the mouse position. - Call
GetDeviceStateto 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_CREATEDEVICEm_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
CDirectInput1Viewassociated withIDD_DIRECTINPUT.
Make sure you select CFormView as the parent class type. - Create a Message Map for
IDC_CREATEDEVICEcallOnCreateDevice, so when the user press the Create Device Button theOnCreatedevicemethod is invoked. - Add the following defines to the
CDirectInput1View.hfile.#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.cppsource.
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.cppsource 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();
}
}
Related articles
Related discussion
-
conting repeated words
by Slicksim (2 replies)
-
Can somebody help: CAsyncSOcket class (Client-server networking)
by Mohammad Rastkar (3 replies)
-
custom progress bar in a datagridview with threading
by konikula (1 replies)
-
Calling C++ DLL from C#
by Thushan Fernando (1 replies)
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!
This thread is for discussions of Direct Input 8.