您的位置:首页 > 其它

自己实现的一个directxinput键盘的封装

2009-06-11 11:17 357 查看
前段时间花了点时间研究directx9,写了点代码,在这里做个记录。

以下是自己封装的keyboard的代码:

cKbState类,保存每次查询的键盘按键信息,提供某个键被按下等查询。

首先是cKbState.h

// cKbState.h: interface for the cKbState class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CKBSTATE_H__6EBDD358_7819_4205_953B_F6DB3D58200F__INCLUDED_)
#define AFX_CKBSTATE_H__6EBDD358_7819_4205_953B_F6DB3D58200F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class cKbState
{
public:
bool Is_KeyPressed(int key);
bool Is_KeyUp(int key);
bool Is_KeyDown(int key);
cKbState();
virtual ~cKbState();
char _key_buffer[256];

};
#endif // !defined(AFX_CKBSTATE_H__6EBDD358_7819_4205_953B_F6DB3D58200F__INCLUDED_)


cKbState.cpp

// cKbState.cpp: implementation of the cKbState class.
//
//////////////////////////////////////////////////////////////////////
#include "cKbState.h"
#include "InputCommon.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
cKbState::cKbState()
{
}
cKbState::~cKbState()
{

}
bool cKbState::Is_KeyDown(int key)
{
return (_key_buffer[key] & 0x80 ?true:false);
}
bool cKbState::Is_KeyUp(int key)
{
return (_key_buffer[key] & 0x80 ?false:true);
}
bool cKbState::Is_KeyPressed(int key)
{
return (_key_buffer[key] & 0x80)?true:false;
}


cKeyboard类,directxinput的一些初始化和释放资源的工作在这里做。

cKeyboard.h

// cKeyboard.h: interface for the cKeyboard class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CKEYBOARD_H__2F31F3FC_DB2D_4FD9_AC3C_481E812B47B4__INCLUDED_)
#define AFX_CKEYBOARD_H__2F31F3FC_DB2D_4FD9_AC3C_481E812B47B4__INCLUDED_
#include "cKbState.h"	// Added by ClassView
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "InputCommon.h"
#include "cKbState.h"

class cKeyboard
{
public:
cKbState Get_kbState();
void Acquire();
void Unacquire();
cKeyboard();
virtual ~cKeyboard();
bool Init_Keyboard(HINSTANCE instance,HWND hwnd);
void Release();
bool Initialized;
private:

void _Release();
bool _Create_DirectInput(HINSTANCE instance);
bool _Create_keyboard(HWND hwnd);
LPDIRECTINPUT8 g_directinput;
LPDIRECTINPUTDEVICE8 _keyboard;

};
#endif // !defined(AFX_CKEYBOARD_H__2F31F3FC_DB2D_4FD9_AC3C_481E812B47B4__INCLUDED_)


cKeyboard.cpp

// cKeyboard.cpp: implementation of the cKeyboard class.
//
//////////////////////////////////////////////////////////////////////
#include "cKeyboard.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
cKeyboard::cKeyboard()
{
}
cKeyboard::~cKeyboard()
{
_Release();
}
bool cKeyboard::Init_Keyboard(HINSTANCE instance,HWND hwnd)
{
if (!_Create_DirectInput(instance))
{
return false;
}
if(!_Create_keyboard(hwnd)){
return false;
}
Initialized=true;
return true;

}
void cKeyboard::_Release()
{
if (_keyboard)
{
_keyboard->Unacquire();
}
Safe_Release(_keyboard);
Safe_Release(g_directinput);
}
void cKeyboard::Acquire()
{
_keyboard->Acquire();
}
cKbState cKeyboard::Get_kbState()
{
cKbState kbState;
ZeroMemory(kbState._key_buffer,sizeof(char)*255);
if(DIERR_INPUTLOST==_keyboard->GetDeviceState(sizeof(kbState._key_buffer),(LPVOID)kbState._key_buffer))
{
_keyboard->Acquire();
if(FAILED(_keyboard->GetDeviceState(sizeof(kbState._key_buffer),(LPVOID)kbState._key_buffer)))
{
return kbState;
}
}
return kbState;
}

bool cKeyboard:: _Create_DirectInput(HINSTANCE instance){
if(FAILED(DirectInput8Create(instance,DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&g_directinput,NULL))){
MessageBox(NULL,"Create DirectInput object failed.","ERROR",MB_OK|MB_ICONINFORMATION);
return false;
}
return true;
}
bool cKeyboard:: _Create_keyboard(HWND hwnd){
//Create keyboard device.
if(FAILED(g_directinput->CreateDevice(GUID_SysKeyboard,&_keyboard,NULL))){
MessageBox(NULL,"DirectInput interface create failed.","ERROR",MB_OK|MB_ICONINFORMATION);
return false;
}
//Set the data format for the Microsoft DirectInput device.
if(FAILED(_keyboard->SetDataFormat(&c_dfDIKeyboard))){
MessageBox(NULL,"Set data format with keyboard read mode failed.","ERROR",MB_OK|MB_ICONINFORMATION);
return false;
}

if(FAILED(_keyboard->SetCooperativeLevel(hwnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE))){
MessageBox(NULL,"Set cooperative Level failed.","ERROR",MB_OK|MB_ICONINFORMATION);
return false;
}
if(FAILED(_keyboard->Acquire())){
MessageBox(NULL,"Acquire keyboard access failed.","ERROR",MB_OK|MB_ICONINFORMATION);
return false;
}

return true;
}

void cKeyboard::Release(){
_Release();
}
void cKeyboard::Unacquire(){
_keyboard->Unacquire();
}


使用方法:

定义全局变量:

//键盘

cKeyboard g_keyboard;

//键盘状态

cKbState g_kbstate;

初始化:

g_keyboard.Init_Keyboard(inst,hwnd);//inst为程序实例,hwnd为窗口句柄

使用:如

g_kbstate=g_keyboard.Get_kbState();

if(g_kbstate.Is_KeyPressed(DIK_LEFT) )

g_camera.yaw(-1.0f * time_delta);

释放:

g_keyboard.Release();

解决切换活动窗口后重新获取键盘资源的问题,需要在窗口过程函数里做设置,代码如下:

LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM word_param, LPARAM long_param)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_KEYDOWN:
if(word_param == VK_ESCAPE)
DestroyWindow(hwnd);
if(word_param == VK_SPACE)
g_draw_triangle = !g_draw_triangle;
break;
case WM_ACTIVATE:
if(g_keyboard.Initialized)
{
g_keyboard.Acquire();
}
if (g_mouse.Initialized)
{
g_mouse.Acquire();
}

break;
}

return DefWindowProc(hwnd, msg, word_param, long_param);
}


需要处理WM_ACTIVATE消息时重新请求键盘资源。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: