#include <dinput.h>
class CDInput
{
public:
int DI_Init(HINSTANCE h, HWND hwnd );
int DI_Shutdown();
int WINAPI GetKey(); //key input
private:
LPDIRECTINPUT7 DI;
LPDIRECTINPUTDEVICE7 DI_Key;
BYTE buffer[256];
};
int WINAPI CDInput::GetKey()
{
HRESULT hr = DI_Key->GetDeviceState(sizeof(buffer), (LPVOID)&buffer);
if(buffer[DIK_ESCAPE] & 0x80)
return true;
else
return false;
}
CDInput::DI_Init(HINSTANCE h, HWND hwnd )
{
// Create Direct Input
if ( DirectInputCreateEx( h, // Window Instance
DIRECTINPUT_VERSION, // Direct Input Version
IID_IDirectInput7, // Version 7
(void**)&DI, // Direct Input
NULL ) )
{
return(false); // Couldn''t Initialize Direct Input
}
// Create The Keyboard Device
if ( DI->CreateDeviceEx( GUID_SysKeyboard, // Define Which Device To Create (KeyBoard,Mouse Or Joystick)
IID_IDirectInputDevice7, // Version 7
(void**)&DI_Key, // KeyBoard Device
NULL ) )
{
// Couldn''t Create The Keyboard Device
return(false); // Failed, Return False
}
if ( DI_Key->SetDataFormat(&c_dfDIKeyboard) ) // Set The Keyboard Data Format
{
// Couldn''t Set The Data Format
return(false); // Failed, Return False
}
// Set The Cooperative Level
if ( DI_Key->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE) )
{
// Couldn''t Set The Cooperative Level
return(false); // Failed, Return False
}
if (DI_Key) // Did We Create The Keyboard Device?
DI_Key->Acquire(); // If So, Acquire It
else
return(false); // If Not, Return False
return true;
}
CDInput::DI_Shutdown()
{
if (DI)
{
if (DI_Key)
{
DI_Key->Unacquire();
DI_Key->Release();
DI_Key = NULL;
}
DI->Release();
DI = NULL;
}
return true;
} |
www.polarisoft.n3.net