📄 diutil.cpp
字号:
if( FAILED( hr = m_pDI->CreateDevice( GUID_SysKeyboard, &m_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 = m_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 = m_pKeyboard->SetCooperativeLevel( hWnd, dwCoopFlags );
if( hr == DIERR_UNSUPPORTED && !bForeGround && bExclusive )
{
FreeDirectInput();
MessageBox( hWnd, _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 = DEFINE_BUFFER_SIZE; // Arbitary buffer size
if( FAILED( hr = m_pKeyboard->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph ) ) )
return hr;
}
// Acquire the newly created device
m_pKeyboard->Acquire();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: ReadImmediateData()
// Desc: Read the input device's state when in immediate mode and display it.
//-----------------------------------------------------------------------------
HRESULT CDirectInputKeyBoard::ReadImmediateData(DKey& keys)
{
HRESULT hr;
BYTE diks[256]; // DirectInput keyboard state buffer
int i;
if( NULL == m_pKeyboard )
return S_OK;
// Get the input's device state, and put the state in dims
ZeroMemory( &diks, sizeof(diks) );
while(1)
{
hr = m_pKeyboard->GetDeviceState( sizeof(diks), &diks );
if( FAILED(hr) )
{
hr = m_pKeyboard->Acquire();
while( hr == DIERR_INPUTLOST )
hr = m_pKeyboard->Acquire();
continue;
}
break;
}
int j=0;
for( i = 0; i < 256; i++ )
{
if( diks[i] & 0x80 )
{
keys[j++]=i;
}
}
return S_OK;
}
BOOL CDirectInputKeyBoard::WaitKey(BYTE key,DWORD dwDelay)
{
if(timeGetTime()-m_dwOldTimeKey[key]<dwDelay)
return false;
m_dwOldTimeKey[key]=timeGetTime();
HRESULT hr;
BYTE diks[256];
int i;
if( NULL == m_pKeyboard )
return false;
ZeroMemory( &diks, sizeof(diks) );
while(1)
{
hr = m_pKeyboard->GetDeviceState( sizeof(diks), &diks );
if( FAILED(hr) )
{
hr = m_pKeyboard->Acquire();
while( hr == DIERR_INPUTLOST )
hr = m_pKeyboard->Acquire();
continue;
}
break;
}
for( i = 0; i < 256; i++ )
{
if( diks[i] & 0x80 )
{
if(i==key)
return true;
}
}
return false;
}
BYTE CDirectInputKeyBoard::ReadLastBufferKey(DWORD dwDelay)
{
if(timeGetTime()-m_dwOldTime<dwDelay)
return 0;
m_dwOldTime=timeGetTime();
HRESULT hr;
BYTE diks[256]; // DirectInput keyboard state buffer
int i;
if( NULL == m_pKeyboard )
return S_OK;
ZeroMemory( &diks, sizeof(diks) );
while(1)
{
hr = m_pKeyboard->GetDeviceState( sizeof(diks), &diks );
if( FAILED(hr) )
{
hr = m_pKeyboard->Acquire();
while( hr == DIERR_INPUTLOST )
hr = m_pKeyboard->Acquire();
continue;
}
break;
}
BYTE LastKey=0;
for( i = 0; i < 256; i++ )
{
if( diks[i] & 0x80 )
{
LastKey=i;
}
}
return LastKey;
}
CDirectInputMouse::CDirectInputMouse()
{
m_pDI=NULL;
m_pDevMouse=NULL;
}
CDirectInputMouse::~CDirectInputMouse()
{
FreeDirectInput();
}
void CDirectInputMouse::FreeDirectInput()
{
if(m_pDevMouse)
m_pDevMouse->Unacquire();
SAFE_RELEASE(m_pDevMouse);
SAFE_RELEASE(m_pDI);
}
HRESULT CDirectInputMouse::CreateMouse(HWND hWnd,BOOL bForeground,BOOL bExclusive,BOOL bImmediate)
{
HRESULT hr;
DWORD dwCoopFlags;
// Cleanup any previous call first
FreeDirectInput();
if( bExclusive )
dwCoopFlags = DISCL_EXCLUSIVE;
else
dwCoopFlags = DISCL_NONEXCLUSIVE;
if( bForeground )
dwCoopFlags |= DISCL_FOREGROUND;
else
dwCoopFlags |= DISCL_BACKGROUND;
// Create a DInput object
if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&m_pDI, NULL ) ) )
return hr;
// Obtain an interface to the system mouse device.
if( FAILED( hr = m_pDI->CreateDevice( GUID_SysMouse, &m_pDevMouse, NULL ) ) )
return hr;
// Set the data format to "mouse 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 a
// DIMOUSESTATE2 structure to IDirectInputDevice::GetDeviceState.
if( FAILED( hr = m_pDevMouse->SetDataFormat( &c_dfDIMouse2 ) ) )
return hr;
// Set the cooperativity level to let DirectInput know how
// this device should interact with the system and with other
// DirectInput applications.
hr = m_pDevMouse->SetCooperativeLevel( hWnd, dwCoopFlags );
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 SAMPLE_BUFFER_SIZE (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 = DEFINE_BUFFER_SIZE; // Arbitary buffer size
if( FAILED( hr = m_pDevMouse->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph ) ) )
return hr;
}
// Acquire the newly created device
m_pDevMouse->Acquire();
// Set a timer to go off 12 times a second, to read input
// Note: Typically an application would poll the mouse
// much faster than this, but this slow rate is simply
// for the purposes of demonstration
return S_OK;
}
BOOL CDirectInputMouse::IsLeftButtonDown()
{
HRESULT hr;
DIMOUSESTATE2 dims2; // DirectInput mouse state structure
if( NULL == m_pDevMouse )
return false;
// Get the input's device state, and put the state in dims
ZeroMemory( &dims2, sizeof(dims2) );
hr = m_pDevMouse->GetDeviceState( sizeof(DIMOUSESTATE2), &dims2 );
if( FAILED(hr) )
{
// DirectInput may be telling us that the input stream has been
// interrupted. We aren't tracking any state between polls, so
// we don't have any special reset that needs to be done.
// We just re-acquire and try again.
// If input is lost then acquire and keep trying
hr = m_pDevMouse->Acquire();
while( hr == DIERR_INPUTLOST )
hr = m_pDevMouse->Acquire();
}
return (dims2.rgbButtons[0] & 0x80) ? true : false;
}
BOOL CDirectInputMouse::IsRightButtonDown()
{
HRESULT hr;
DIMOUSESTATE2 dims2; // DirectInput mouse state structure
if( NULL == m_pDevMouse )
return false;
// Get the input's device state, and put the state in dims
ZeroMemory( &dims2, sizeof(dims2) );
hr = m_pDevMouse->GetDeviceState( sizeof(DIMOUSESTATE2), &dims2 );
if( FAILED(hr) )
{
// DirectInput may be telling us that the input stream has been
// interrupted. We aren't tracking any state between polls, so
// we don't have any special reset that needs to be done.
// We just re-acquire and try again.
// If input is lost then acquire and keep trying
hr = m_pDevMouse->Acquire();
while( hr == DIERR_INPUTLOST )
hr = m_pDevMouse->Acquire();
}
return (dims2.rgbButtons[1] & 0x80) ? true : false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -