?? 鍵盤獲取.cpp
字號:
// 鍵盤獲取.cpp : 定義應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include "鍵盤獲取.h"
#define MAX_LOADSTRING 100
#include <dinput.h>
#include <dinputd.h>
#pragma comment(lib,"dinput8.lib")
#pragma comment(lib,"dxguid.lib")
#define MK_MIN 0x100
#define MK_LEFT 0x100 //>255
#define MK_RIGHT 0x101
#define MK_MID 0x102
#define SAFE_RELEASE(p) if(p) {p->Release();p=NULL;}
// 全局變量:
HINSTANCE hInst; // 當(dāng)前實(shí)例
TCHAR szTitle[MAX_LOADSTRING]; // 標(biāo)題欄文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口類名
HDC memDC;//定義一個兼容DC
HBITMAP hBitmap;
BITMAP bmp;
//在DirectX9.0c的SDK中的DirectInput實(shí)際是8.0版本
LPDIRECTINPUT8 m_pDInput;//DirectInput8的接口指針
LPDIRECTINPUTDEVICE8 m_pDInputKB;//設(shè)備指針
char m_strKeyState[256];
char m_strKeyStateOld[256];
int x,y;
// 此代碼模塊中包含的函數(shù)的前向聲明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
LRESULT GetIsKeyDown(DWORD keycode)
{
if(keycode<MK_MIN)
return (0x80 & m_strKeyState[keycode]) | m_strKeyStateOld[keycode] >>4 ;
};
LRESULT Update(void)
{
//更新之前記錄一下上一次的狀態(tài)
memcpy(m_strKeyStateOld,m_strKeyState,sizeof(m_strKeyState));
if(DI_OK!=m_pDInputKB->GetDeviceState(sizeof(m_strKeyState),m_strKeyState))
{
memset(&m_strKeyState,0,sizeof(m_strKeyState)); //清除上一次的狀態(tài)
m_pDInputKB->Acquire();
}
return DI_OK;
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: 在此放置代碼。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 執(zhí)行應(yīng)用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY);
// 主消息循環(huán):
ZeroMemory(&msg,sizeof(msg));
while (msg.message!=WM_QUIT)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Update();
}
}
return (int) msg.wParam;
}
//
// 函數(shù): MyRegisterClass()
//
// 目的: 注冊窗口類。
//
// 注釋:
//
// 僅當(dāng)希望在已添加到 Windows 95 的
// “RegisterClassEx”函數(shù)之前此代碼與 Win32 系統(tǒng)兼容時,
// 才需要此函數(shù)及其用法。調(diào)用此函數(shù)
// 十分重要,這樣應(yīng)用程序就可以獲得關(guān)聯(lián)的
// “格式正確的”小圖標(biāo)。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MY);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCTSTR)IDC_MY;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// 函數(shù): InitInstance(HANDLE, int)
//
// 目的: 保存實(shí)例句柄并創(chuàng)建主窗口
//
// 注釋:
//
// 在此函數(shù)中,我們在全局變量中保存實(shí)例句柄并
// 創(chuàng)建和顯示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 將實(shí)例句柄存儲在全局變量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函數(shù): WndProc(HWND, unsigned, WORD, LONG)
//
// 目的: 處理主窗口的消息。
//
// WM_COMMAND - 處理應(yīng)用程序菜單
// WM_PAINT - 繪制主窗口
// WM_DESTROY - 發(fā)送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜單選擇:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
hBitmap = NULL;
//步驟(1):創(chuàng)建DirectInput8對象
if(DI_OK!=DirectInput8Create( hInst,DIRECTINPUT_VERSION,
IID_IDirectInput8,(LPVOID*)&m_pDInput,NULL))
MessageBox(hWnd,"創(chuàng)建DirectInput 對象失敗!","ERROR",MB_ICONERROR|MB_OK);
//步驟(2):創(chuàng)建鍵盤設(shè)備
if(DI_OK!=m_pDInput->CreateDevice(GUID_SysKeyboard,&m_pDInputKB,NULL))
MessageBox(hWnd,"創(chuàng)建鍵盤設(shè)備失敗!","ERROR",MB_ICONERROR|MB_OK);
//步驟(3):設(shè)置數(shù)據(jù)格式
if(DI_OK!= m_pDInputKB->SetDataFormat(&c_dfDIKeyboard))
MessageBox(hWnd,"設(shè)置鍵盤數(shù)據(jù)格式失敗!","ERROR",MB_ICONERROR|MB_OK);
//步驟(4):設(shè)置設(shè)備的協(xié)調(diào)層級
m_pDInputKB->SetCooperativeLevel(hWnd,DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
//步驟(5):獲取設(shè)備
m_pDInputKB->Acquire();
//步驟(6):清除狀態(tài),并且獲取輸入的數(shù)據(jù)(在Updata這個函數(shù)中)
memset(&m_strKeyState,0,sizeof(m_strKeyState));
memset(&m_strKeyStateOld,0,sizeof(m_strKeyState));
SetTimer(hWnd,1,20,NULL);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
if(!hBitmap)
{
memDC = CreateCompatibleDC(hdc);//創(chuàng)建內(nèi)存DC
hBitmap = (HBITMAP)LoadImage(hInst,"ball.bmp" ,IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);//高色位圖也沒關(guān)系
SelectObject(memDC,hBitmap);//選入新位圖入內(nèi)存DC
GetObject(hBitmap, sizeof(BITMAP), &bmp);
}
BitBlt(hdc,x,y,bmp.bmWidth,bmp.bmHeight,memDC,0,0,SRCCOPY);//將內(nèi)存DC中的數(shù)據(jù)復(fù)制到顯示DC
ReleaseDC(hWnd,memDC);
EndPaint(hWnd, &ps);
break;
case WM_TIMER:
RECT rect;
GetClientRect(hWnd, &rect);
switch(wParam)
{
case 1:
if(0x80&m_strKeyState[DIK_RIGHT])
if(x+80 > rect.right)
x = rect.right - 80;
else
x+=10;
if(0x80&m_strKeyState[DIK_LEFT])
if(x-10 < 0)
x = 0;
else
x-=10;
if(0x80&m_strKeyState[DIK_UP])
if(y-10 < 0)
y = 0;
else
y-=10;
if(0x80&m_strKeyState[DIK_DOWN])
if(y+80 > rect.bottom)
y = rect.bottom-80;
else
y+=10;
if(0x80&m_strKeyState[DIK_ESCAPE])
{
KillTimer(hWnd,1);
PostMessage(hWnd,WM_CLOSE,NULL,NULL);
}
if(0x80&m_strKeyState[DIK_A])
MessageBox(NULL,"DirectInput","",MB_OK);
}
InvalidateRect(hWnd,&rect,FALSE);
break;
case WM_KEYDOWN:
if(wParam=='A')
MessageBox(NULL,"Message","",MB_OK);
break;
case WM_DESTROY:
m_pDInputKB->Unacquire();
SAFE_RELEASE(m_pDInputKB);
SAFE_RELEASE(m_pDInput);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “關(guān)于”框的消息處理程序。
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -