自己实现的一个directxinput键盘的封装
2009-06-11 11:17
357 查看
前段时间花了点时间研究directx9,写了点代码,在这里做个记录。
以下是自己封装的keyboard的代码:
cKbState类,保存每次查询的键盘按键信息,提供某个键被按下等查询。
首先是cKbState.h
cKbState.cpp
cKeyboard类,directxinput的一些初始化和释放资源的工作在这里做。
cKeyboard.h
cKeyboard.cpp
使用方法:
定义全局变量:
//键盘
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();
解决切换活动窗口后重新获取键盘资源的问题,需要在窗口过程函数里做设置,代码如下:
需要处理WM_ACTIVATE消息时重新请求键盘资源。
以下是自己封装的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消息时重新请求键盘资源。
相关文章推荐
- ios-数据库-sqlite-数据库得封装(自己写得,有很多不严谨)最后还有一个单例得实现
- JS +XML +Jquery 实现三级联动菜单,自己封装的一个对象方便使用
- 自己封装一个类借助实现数据库加载及夜间模式
- 创建一个类似发送短信时的软键盘,是需要自己实现还是改变软键盘类型?
- 封装实现一个自己的tabbar
- 封装实现一个自己的tabbar
- 自己封装一个readline函数实现服务器客户端回射
- 整理一个自己用的Ajax例子,封装,调用!
- 自己用 Netty 实现一个简单的 RPC
- java RMI一个例子以及模仿RMI的原理实现一个自己的RMI
- 自己封装实现的仿GridView的列表控件
- Objective-C如何自己实现一个基于数组下标的属性访问模式
- 自己实现一个一致性 Hash 算法
- 毕加索的艺术——Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选
- 对Guava Cache的封装和使用(包括一个管理页面实现了查看统计信息、情况、查看记录等)
- 创建一个数组, 实现函数init()初始化数组、 实现empty()清空数组、 实现reverse()函数完成数组元素的逆置。 要求:自己设计函数的参数,返回值。
- 仿照jquery封装一个自己的js库(一)
- 自己实现一个RPC框架
- 自己动手系列——实现一个简单的LinkedList
- 实现一个自己的网络浏览器