Insert into Source: DirectInput1view.cpp
#include <dinput.h>
#include <basetsd.h>
The follow methods need to be added to the directinput1view.cpp
and directinput1view.h
file:
directinput1view.h
HRESULT CDirectInput1View::CreateKeyBoardDevice(HWND hDlg);
directinput1view.cpp Source
HRESULT CDirectInput1View::CreateKeyBoardDevice(HWND hDlg)
{
HRESULT hr;
BOOL bExclusive;
BOOL bForeground;
BOOL bImmediate;
BOOL bDisableWindowsKey;
DWORD dwCoopFlags;
#ifdef _WIN64
HINSTANCE hInst = (HINSTANCE) GetWindowLongPtr( hDlg, GWLP_HINSTANCE );
#else
HINSTANCE hInst = (HINSTANCE) GetWindowLong( hDlg, GWL_HINSTANCE );
#endif
// Cleanup any previous call first. The timer is used to output
// Keyboard events
//KillTimer(0 );
FreeDirectInput();
UpdateData(TRUE);
// Determine where the buffer would like to be allocated
bExclusive = m_exclusive_control.GetCheck();
bForeground = m_foreground_control.GetCheck();
bImmediate = m_immediate_control.GetCheck();
bDisableWindowsKey = m_windowskey_control.GetCheck();
if( bExclusive )
dwCoopFlags = DISCL_EXCLUSIVE;
else
dwCoopFlags = DISCL_NONEXCLUSIVE;
if( bForeground )
dwCoopFlags |= DISCL_FOREGROUND;
else
dwCoopFlags |= DISCL_BACKGROUND;
// Disabling the windows key is only allowed only if we are in foreground nonexclusive
if( bDisableWindowsKey && !bExclusive && bForeground )
dwCoopFlags |= DISCL_NOWINKEY;
// Create a DInput object
if(FAILED(hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&g_pDI, NULL)))
return hr;
// Obtain an interface to the system keyboard device.
if( FAILED( hr = g_pDI->CreateDevice( GUID_SysKeyboard, &g_pKeyboard, NULL ) ) )
return hr;
// Set the data format to "keyboard format" - a predefined data format
//
// A data format specifies which controls on a device we
// are interested in, and how they should be reported.
//
// This tells DirectInput that we will be passing an array
// of 256 bytes to IDirectInputDevice::GetDeviceState.
if( FAILED( hr = g_pKeyboard->SetDataFormat( &c_dfDIKeyboard ) ) )
return hr;
// Set the cooperativity level to let DirectInput know how
// this device should interact with the system and with other
// DirectInput applications.
hr = g_pKeyboard->SetCooperativeLevel( hDlg, dwCoopFlags );
if( hr == DIERR_UNSUPPORTED && !bForeground && bExclusive )
{
FreeDirectInput();
MessageBox( _T("SetCooperativeLevel() returned DIERR_UNSUPPORTED.\n")
_T("For security reasons, background
exclusive keyboard\n")
_T("access is not allowed."), _T("Keyboard"),
MB_OK );
return S_OK;
}
if( FAILED(hr) )
return hr;
if( !bImmediate )
{
// IMPORTANT STEP TO USE BUFFERED DEVICE DATA!
//
// DirectInput uses unbuffered I/O (buffer size = 0) by default.
// If you want to read buffered data, you need to set a nonzero
// buffer size.
//
// Set the buffer size to DINPUT_BUFFERSIZE (defined above) elements.
//
// The buffer size is a DWORD property associated with the device.
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = SAMPLE_BUFFER_SIZE;
// Arbitary buffer size
if( FAILED( hr = g_pKeyboard->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph
) ) )
return hr;
}
// Acquire the newly created device
g_pKeyboard->Acquire();
// Set a timer to go off 12 times a second, to read input
// Note: Typically an application would poll the keyboard
// much faster than this, but this slow rate is simply
// for the purposes of demonstration
SetTimer( 0, 1000 / 12, NULL );
return (S_OK);
}
Comments