DirectInput:
Key codes are DIK_A, DIK_HOME, DIK_PRIOR, etc.
Here's a DirectInput class ripped from the Two King's tutorials. (Link in the sticky)
Code:
#ifndef cinput_h
#define cinput_h
#include "main.h"
#define D3DFVF_CURSORVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
class CInput
{
public:
CInput(void);
~CInput(void);
bool InitDirectInput(void);
bool InitKeyboard(void);
bool InitMouse(void);
bool Update(void);
bool KeyPressed(int);
int MouseButtonDown(int index)
{
if (index<0 || index>NUMMOUSEBUTTONS)
return 0;
return (m_Button[index]);
}
enum { NUMMOUSEBUTTONS = 3};
long* GetRelPosition(void) {return m_relposition;}
private:
LPDIRECTINPUT8 m_pDirectInputObject;
LPDIRECTINPUTDEVICE8 m_pDirectInputKeyboardDevice,
m_pDirectInputMouseDevice;
char m_KeyBuffer[256];
char m_Button[NUMMOUSEBUTTONS];
long m_relposition[3];
};
#endif
Code:
#include "main.h"
CInput::CInput(void)
{
m_pDirectInputObject = NULL;
m_pDirectInputKeyboardDevice = NULL;
if(!InitDirectInput())
g_App.SetD3DStatus(false);
else if(!InitKeyboard()||!InitMouse())
g_App.SetD3DStatus(false);
}
CInput::~CInput(void)
{
if(m_pDirectInputKeyboardDevice)
{
m_pDirectInputKeyboardDevice->Unacquire();
m_pDirectInputKeyboardDevice->Release();
m_pDirectInputKeyboardDevice = NULL;
}
if(m_pDirectInputMouseDevice)
{
m_pDirectInputMouseDevice->Unacquire();
m_pDirectInputMouseDevice->Release();
m_pDirectInputMouseDevice = NULL;
}
if(m_pDirectInputObject)
{
m_pDirectInputObject->Release();
m_pDirectInputObject = NULL;
}
}
bool CInput::InitDirectInput(void)
{
if(FAILED(DirectInput8Create( GetModuleHandle(NULL),DIRECTINPUT_VERSION, IID_IDirectInput8,
(void**)&m_pDirectInputObject,NULL)))
{
g_Error.Log("DirectInput8Create() failed in CInput::InitDirectInput()");
MessageBox(g_App.GetWindowHandle(),"DirectInput8Create() failed!","InitDirectInput()",MB_OK);
return false;
}
return true;
}
bool CInput::InitKeyboard(void)
{
if(FAILED(m_pDirectInputObject->CreateDevice(GUID_SysKeyboard,&m_pDirectInputKeyboardDevice,NULL)))
{
g_Error.Log("CreateDevice() failed in CInput::InitKeyboard()");
MessageBox(g_App.GetWindowHandle(),"CreateDevice() failed!","InitKeyboard()",MB_OK);
return false;
}
if(FAILED(m_pDirectInputKeyboardDevice->SetDataFormat(&c_dfDIKeyboard)))
{
g_Error.Log("SetDataFormat() failed in CInput::InitKeyboard()");
MessageBox(g_App.GetWindowHandle(),"SetDataFormat() failed!","InitKeyboard()",MB_OK);
return false;
}
if(FAILED(m_pDirectInputKeyboardDevice->SetCooperativeLevel(g_App.GetWindowHandle(),
DISCL_BACKGROUND|DISCL_NONEXCLUSIVE)))
{
g_Error.Log("SetCooperativeLevel() failed in CInput::InitKeyboard()");
MessageBox(g_App.GetWindowHandle(),"SetCooperativeLevel() failed!","InitKeyboard()",MB_OK);
return false;
}
if(FAILED(m_pDirectInputKeyboardDevice->Acquire()))
{
g_Error.Log("Acquire() failed in CInput::InitKeyboard()");
MessageBox(g_App.GetWindowHandle(),"Acquire() failed!","InitKeyboard()",MB_OK);
return false;
}
return true;
}
bool CInput::InitMouse(void)
{
DIDEVCAPS MouseCapabilities;
if(FAILED(m_pDirectInputObject->CreateDevice(GUID_SysMouse,&m_pDirectInputMouseDevice,NULL)))
{
g_Error.Log("CreateDevice() failed in CInput::InitMouse()");
MessageBox(g_App.GetWindowHandle(),"CreateDevice() failed!","InitMouse()",MB_OK);
return false;
}
if(FAILED(m_pDirectInputMouseDevice->SetDataFormat(&c_dfDIMouse)))
{
g_Error.Log("SetDataFormat() failed in CInput::InitMouse()");
MessageBox(g_App.GetWindowHandle(),"SetDataFormat() failed!","InitMouse()",MB_OK);
return false;
}
if(FAILED(m_pDirectInputMouseDevice->SetCooperativeLevel(g_App.GetWindowHandle(),
DISCL_BACKGROUND|DISCL_NONEXCLUSIVE)))
{
g_Error.Log("SetCooperativeLevel() failed in CInput::InitMouse()");
MessageBox(g_App.GetWindowHandle(),"SetCooperativeLevel() failed!","InitMouse()",MB_OK);
return false;
}
if(FAILED(m_pDirectInputMouseDevice->Acquire()))
{
g_Error.Log("Acquire() failed in CInput::InitMouse()");
MessageBox(g_App.GetWindowHandle(),"Acquire() failed!","InitMouse()",MB_OK);
return false;
}
MouseCapabilities.dwSize = sizeof(MouseCapabilities);
m_pDirectInputMouseDevice->GetCapabilities(&MouseCapabilities);
if(!(MouseCapabilities.dwFlags&DIDC_ATTACHED))
{
g_Error.Log("Mouse is not attached to system.");
MessageBox(g_App.GetWindowHandle(),"Mouse is currently not attached! \nClick here to continue","InitMouse()",MB_OK);
return false;
}
return true;
}
bool CInput::Update(void)
{
if(FAILED(m_pDirectInputKeyboardDevice->GetDeviceState(sizeof(m_KeyBuffer),(LPVOID)&m_KeyBuffer)))
{
g_Error.Log("GetDeviceState() failed in CInput::Update()");
MessageBox(g_App.GetWindowHandle(),"GetDeviceState() failed!","Update()",MB_OK);
return false;
}
char done=0;
int q;
DIMOUSESTATE dims;
HRESULT hr;
memset(&dims,0,sizeof(DIMOUSESTATE));
if(!m_pDirectInputMouseDevice)
return false;
while(!done)
{
hr=m_pDirectInputMouseDevice->GetDeviceState(sizeof(DIMOUSESTATE),&dims);
if (FAILED(hr))
{
if (hr==DIERR_INPUTLOST||hr==DIERR_NOTACQUIRED)
{
if (FAILED(m_pDirectInputMouseDevice->Acquire()))
done=1;
}
else
done=1;
}
else
done=1;
}
m_relposition[0]=dims.lX;
m_relposition[1]=dims.lY;
m_relposition[2]=dims.lZ;
for (q=0;q<NUMMOUSEBUTTONS;q++)
m_Button[q]=(dims.rgbButtons[q]&0x80);
return true;
}
bool CInput::KeyPressed(int Key)
{
if(m_KeyBuffer[Key] & 0x80)
return true;
return false;
}
Sorry, I was too lazy to zip that and attach it.