D3D键盘输入处理

 

 

#include "HandleKey.h"

 

BOOL InitKeyboard()

{

//创建DirectInput接口对象

HRESULT hr;

hr=DirectInput8Create(g_hInst,DIRECTINPUT_VERSION,

IID_IDirectInput8,(void**)&pIDirectInput,NULL);

if(FAILED(hr))

{

MessageBox(NULL,"建立IDIRECTINPUT8接口对象失败.","警告",MB_OK|MB_ICONINFORMATION);

return false;

}

//使用DirectInput接口函数

hr=pIDirectInput->CreateDevice(GUID_SysKeyboard,&pIDirectInputDevice,NULL);

if(FAILED(hr))

{

MessageBox(NULL,"建立IDIRECTINPUTDEVICE8键盘输入设备对象失败.","警告",MB_OK|MB_ICONINFORMATION);

SafeRelease(pIDirectInput);

return false;

}

//设置DirectInput设备的数据格式

hr=pIDirectInputDevice->SetDataFormat(&c_dfDIKeyboard);

if(FAILED(hr))

{

MessageBox(NULL,"设置键盘的数据读取数据格式失败.","警告",MB_OK|MB_ICONINFORMATION);

SafeRelease(pIDirectInputDevice);

SafeRelease(pIDirectInput);

return false;

}

//设置DirectInput设备的协调级别

hr=pIDirectInputDevice->SetCooperativeLevel(g_hWnd,DISCL_FOREGROUND|DISCL_EXCLUSIVE);

if(FAILED(hr))

{

MessageBox(NULL,"设置协调级别失败.","警告",MB_OK|MB_ICONINFORMATION);

SafeRelease(pIDirectInputDevice);

SafeRelease(pIDirectInput);

return false;

}

//获取输入设备的访问权

hr=pIDirectInputDevice->Acquire();

if(FAILED(hr))

{

MessageBox(NULL,"取得键盘设备的访问权失败.","警告",MB_OK|MB_ICONINFORMATION);

SafeRelease(pIDirectInputDevice);

SafeRelease(pIDirectInput);

return false;

}

//键盘缓冲区清零

ZeroMemory(keyBuffer,sizeof(char)*256);

return TRUE;

}

 

BOOL IsKeyPressed(int key)

//key=DIK_RIGHT,DIK_LEFT,DIK_A,.....

HRESULT hr;

hr=pIDirectInputDevice->GetDeviceState(sizeof(keyBuffer),(LPVOID)keyBuffer);

if(hr==DIERR_INPUTLOST)

{

pIDirectInputDevice->Acquire();  //重新获取键盘的使用权

hr=pIDirectInputDevice->GetDeviceState(sizeof(keyBuffer),(LPVOID)keyBuffer);

if(FAILED(hr))

MessageBox(NULL,"取得键盘按键状态失败.","警告",MB_OK|MB_ICONINFORMATION);

return false;

}

}

if(keyBuffer[key] & 0x80)

{

return true;

}

return false;

}

 

void ReleaseCOMObject <script src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" type="text/javascript"></script> <script src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" type="text/javascript"></script> ()

{

if(pIDirectInputDevice)

{

pIDirectInputDevice->Unacquire();   //释放DirectInput设备的使用权

        SafeRelease(pIDirectInputDevice); //释放DirectInput设备对象

   SafeRelease(pIDirectInput);        //释放DirectInput对象

}

}

 


 

 

 

 

#include <dinput.h>

 

 

extern HINSTANCE g_hInst;

extern HWND g_hWnd;

#define SafeRelease(pObject) if(pObject!=NULL){ pObject->Release(); pObject=NULL;}

 

LPDIRECTINPUT8 pIDirectInput=NULL;

LPDIRECTINPUTDEVICE8 pIDirectInputDevice=NULL;

char keyBuffer[256];  //键盘缓冲区

BOOL InitKeyboard();

BOOL IsKeyPressed(int);

void ReleaseCOMObject();

 

 

 


 

 

 

#include <windows.h>

#include <dinput.h>

 

#pragma    comment(lib,"dxguid.lib")

#pragma    comment(lib,"dinput8.lib")

#pragma    comment(lib,"odbc32.lib")

#pragma    comment(lib,"odbccp32.lib")

 

//定义全局变量

HINSTANCE g_hInst;

HWND g_hWnd;

const TCHAR g_szWndClass[]="Game";

const TCHAR g_szWndTitle[]="DirectInput键盘输入";

WNDCLASSEX g_wcex;

//函数原型声明

ATOM RegWndClass(HINSTANCE);

BOOL CreateWnd(HINSTANCE, int);

LRESULT CALLBACK  WndProc(HWND, UINT, WPARAM, LPARAM);

extern BOOL InitKeyboard();

extern BOOL IsKeyPressed(int);

extern void ReleaseCOMObject();

 

//函数定义

int APIENTRY WinMain(HINSTANCE hInstance,       //传入的窗口句柄

HINSTANCE hPrevInstance,   //已存在的窗口句柄

LPSTR     lpCmdLine,        //传入的命令行参数

int       nCmdShow)         //设置窗口的显示方式

{

MSG msg;

g_hInst=hInstance;

if(!hPrevInstance)

{

//判断是否已有应用程序实例在运行

RegWndClass(hInstance);

}

if(!CreateWnd(hInstance, nCmdShow))

{

return FALSE;

}

 

 

if(!InitKeyboard())

{

return FALSE;

}

while(msg.message!=WM_QUIT)

{

if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

else

{

if(IsKeyPressed(DIK_RIGHT))

{

MessageBox(NULL,"右箭头键按下","按键状态",MB_OK|MB_ICONINFORMATION);

}

if(IsKeyPressed(DIK_LEFT))

{

MessageBox(NULL,"左箭头键按下","按键状态",MB_OK|MB_ICONINFORMATION);

}

if(IsKeyPressed(DIK_D)&IsKeyPressed(DIK_LCONTROL)){

MessageBox(NULL,"Ctrl+D双键按下","按键状态",MB_OK|MB_ICONINFORMATION);

}

if(IsKeyPressed(DIK_A))

{

MessageBox(NULL,"A键按下","按键状态",MB_OK|MB_ICONINFORMATION);

}

if(IsKeyPressed(DIK_ESCAPE))

{

PostQuitMessage(0);

}

}

}

ReleaseCOMObject();

UnregisterClass(g_szWndClass, g_wcex.hInstance);

return msg.wParam;

}

 

ATOM RegWndClass(HINSTANCE hInstance)

{

g_wcex.cbSize         = sizeof(WNDCLASSEX); 

g_wcex.style    = CS_HREDRAW | CS_VREDRAW;  //

g_wcex.lpfnWndProc = (WNDPROC)WndProc;

g_wcex.cbClsExtra = 0;

g_wcex.cbWndExtra = 0;

g_wcex.hInstance = hInstance;

g_wcex.hIcon    = 0;//LoadIcon(hInstance, (LPCTSTR)IDI_WINICON);

g_wcex.hCursor = LoadCursor(NULL, IDC_ARROW);

g_wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

g_wcex.lpszMenuName = 0;

g_wcex.lpszClassName = g_szWndClass;

g_wcex.hIconSm = NULL;

return RegisterClassEx(&g_wcex);

}

 

BOOL CreateWnd(HINSTANCE hInstance, int nCmdShow)

{

 

g_hWnd = CreateWindow(g_szWndClass, g_szWndTitle, WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!g_hWnd)

{

return FALSE;

}

ShowWindow(g_hWnd, nCmdShow);  //显示窗口

UpdateWindow(g_hWnd);  //刷新窗口

return TRUE;

}

 

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

switch (message)

{

case WM_DESTROY:

PostQuitMessage(0);

break;

default:

return DefWindowProc(hWnd,message, wParam, lParam);

}

return 0;

}

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值