#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
()
{
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;
//函數原型聲明
ATOMRegWndClass(HINSTANCE);
BOOLCreateWnd(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;
}