| CDIKeyboard
Click to download the Class Source, you will also need the base class. To use the class, define a global variable :- CDIKeyboard myKeyboard; You will have to supply the HWND for the parent window under which you wish to take Keyboard Control. The class has the following public methods:-
Before obtaining keyboard state information you will need to set the HWND of the parent window of your application. Use PollDevice method before checking the Keyboard state information and IsKeyPressed(key) method to check a particular key has been pressed. The following code snippet can be used to show which keys have been pressed bool m_bInput; myKeyboard.PollDevice(); m_bInput=myKeyboard.IsKeyPressed(DIK_F1)?true:false; The Source Code CDIJoystick Class Header File : DIKeyboard.h // DIKeyboard.h: interface for the CDIKeyboard class.
//
//////////////////////////////////////////////////////////////////////
// DIKeyboard.h: the Keyboard Direct Input class.
//
// Written and Developed by Jason Brooks
// (C) 2000 Jason Brooks
//
// You may use this code freely, however a mention in your credits
// would be nice.
//
// For more information, Bug Reports and so on I can be contacted :-
//
// E-Mail: DirectInput@muckypaws.com
//
// Web: www.muckypaws.com
// ICQ: 9609400
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_DIKEYBOARD_H__4D4177D5_C36A_44A0_80FF_FD46D1A12647__INCLUDED_)
#define AFX_DIKEYBOARD_H__4D4177D5_C36A_44A0_80FF_FD46D1A12647__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "DIBase.h"
#pragma comment (lib, "dxguid.lib")
#pragma comment (lib, "dinput.lib")
class CDIKeyboard : public CDIBase
{
public:
CDIKeyboard(); // Constructor
virtual ~CDIKeyboard(); // Destructor
void SetHWND(HWND); // Overridden SetHWND
void SetHWND(CWnd*); // Overridden SetHWND
bool IsKeyPressed(unsigned char keyname); // Has a specific DI_KEY been pressed.
virtual bool PollDevice(void); // Update the device data.
public:
char m_buffer[256];
protected:
bool InitKeyboard(void);
};
#endif // !defined(AFX_DIKEYBOARD_H__4D4177D5_C36A_44A0_80FF_FD46D1A12647__INCLUDED_)
Implementation File : DIKeyboard.cpp // DIKeyboard.cpp: implementation of the CDIKeyboard class.
//
//////////////////////////////////////////////////////////////////////
// DIKeyboard.cpp: the Keyboard Direct Input class.
//
// Written and Developed by Jason Brooks
// (C) 2000 Jason Brooks
//
// You may use this code freely, however a mention in your credits
// would be nice.
//
// For more information, Bug Reports and so on I can be contacted :-
//
// E-Mail: DirectInput@muckypaws.com
//
// Web: www.muckypaws.com
// ICQ: 9609400
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DIKeyboard.h"
#define KEYDOWN(name,key) (name[key] & 0x80)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDIKeyboard::CDIKeyboard()
{
// Clear buffer information
ZeroMemory(&m_buffer,sizeof(m_buffer));
CDIBase::Initialise();
InitKeyboard();
}
CDIKeyboard::~CDIKeyboard()
{
// Shutdown and UnAcquire!
CDIBase::Shutdown();
}
//////////////////////////////////////////////////////////////////////
//
// This will fail if you have not set the HWND
// Initialise the keyboard
//
// true = Success
// false = Failed
//
//////////////////////////////////////////////////////////////////////
bool CDIKeyboard::InitKeyboard()
{
// Check m_hwnd has been set.
if(!m_hwnd)
{
OutputDebugString("HWND Not set, Can not Initialise Keyboard for this application.\nCall SetHWND before calling this method.\n");
return false;
}
// Keyboard Should Always Be An Attached Device!
// Therefore safe to assume
hr = m_lpDI->CreateDeviceEx(GUID_SysKeyboard, IID_IDirectInputDevice7,
(void**)&m_lpDIDevice, NULL);
if FAILED(hr)
{
CDIBase::Shutdown();
return false;
}
// Now Set The Data Format - Keyboard!
hr = m_lpDIDevice->SetDataFormat(&c_dfDIKeyboard);
if FAILED(hr)
{
CDIBase::Shutdown();
return false;
}
// Set the cooperative level
hr = m_lpDIDevice->SetCooperativeLevel(m_hwnd,
DISCL_FOREGROUND | DISCL_EXCLUSIVE); //DISCL_NONEXCLUSIVE
if FAILED(hr)
{
CDIBase::Shutdown();
return false;
}
m_Initialised=true;
return true; // Successfully Created DI Devices!
}
//////////////////////////////////////////////////////////////////////
//
// Poll the device and update the m_buffer data!
//
//////////////////////////////////////////////////////////////////////
bool CDIKeyboard::PollDevice(void)
{
// Has this object been initialised?
if(!m_Initialised)
{
if(!Initialise())
{
OutputDebugString("Failed To Initialise DX7 Object in CDIKeyboard::PollDevice(void)\n");
return false;
}
if(!InitKeyboard())
{
OutputDebugString("Failed To Initialise Keyboard in CDIKeyboard::PollDevice(void)\n");
return false;
}
}
if(!m_Initialised)
{
OutputDebugString("CDIKeyboard::PollDevice Quitting, No DX7 pointer\n");
return false;
}
// Now Get the Keyboard State to the Keyboard Buffer!
hr = m_lpDIDevice->GetDeviceState(sizeof(m_buffer),(LPVOID)&m_buffer);
if FAILED(hr)
{
OutputDebugString("Failed To Obtain Keyboard Data in CDIKeyboard::PollDevice()\nTrying To Restore\n");
OutputDebugString(GetDIError(hr));
if(!Acquire(true))
{
OutputDebugString("CDIKeyboard::PollDevice Quitting, Could Not Acquire The Device\n");
return false;
}
if(hr==DIERR_INPUTLOST)
{
OutputDebugString("ReInitialising DInput in CDIKeyboard::PollDevice()\n");
CDIBase::ReInitialise();
if(!InitKeyboard())
{
OutputDebugString("Failed To Re-Initialise Keyboard, Quitting CDIKeyboard::PollDevice()\n");
return false;
}
// Now lets try again!
hr = m_lpDIDevice->GetDeviceState(sizeof(m_buffer),(LPVOID)&m_buffer);
if FAILED(hr)
{
OutputDebugString("Could Not Get Keyboard Input From Direct Input!\n");
OutputDebugString(GetDIError(hr));
return false;
}
}
return false;
}
// Success, We've Acquired the device!
return true;
}
//////////////////////////////////////////////////////////////////////
//
// Is a specific Key being pressed?
//
// true = Key Pressed
// false = Key Not Pressed
//
//////////////////////////////////////////////////////////////////////
bool CDIKeyboard::IsKeyPressed(unsigned char keyname)
{
if(!m_Initialised) return false;
if (m_buffer[keyname]&0x80)return true;
return false;
}
//////////////////////////////////////////////////////////////////////
//
// Set or change the HWND to the window with primary focus.
//
//////////////////////////////////////////////////////////////////////
void CDIKeyboard::SetHWND(HWND hwnd)
{
CDIBase::SetHWND(hwnd);
CDIBase::ReInitialise();
InitKeyboard();
}
//////////////////////////////////////////////////////////////////////
//
// Set or change the HWND to the window with primary focus.
//
//////////////////////////////////////////////////////////////////////
void CDIKeyboard::SetHWND(CWnd* cwnd)
{
SetHWND(cwnd->m_hWnd);
}
|
|
|