#include <Windows.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,
LPSTR lpszCmdLine, int nCmdLine)
{
HWND hwnd;
MSG msg;
char lpszClassName[] = "键盘";
char lpszTitle[] = "键盘操作";
WNDCLASS cklx;
cklx.style = CS_HREDRAW | CS_VREDRAW;
cklx.cbClsExtra = 0;
cklx.cbWndExtra = 0;
cklx.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH));
cklx.hCursor = LoadCursor(NULL, IDC_ARROW);
cklx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
cklx.lpfnWndProc = WndProc;
cklx.lpszClassName = lpszClassName;
cklx.lpszMenuName = NULL;
cklx.hInstance = hInstance;
if (!RegisterClass(&cklx))
{
MessageBeep(0);
return false;
}
hwnd = CreateWindow(lpszClassName, lpszTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 400, NULL, NULL, hInstance, NULL);
if (!hwnd)
{
MessageBox(NULL, "创建窗口失败!", "失败!", 0);
return false;
}
ShowWindow(hwnd, nCmdLine);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
static TCHAR str[10][200] = { "" };
static int cur = 0, num = 0;
static TCHAR key_kode[][30] = {
"WM_KEYDOWN","WM_KEYUP",
"WM_CHAR","WM_DEADCHAR",
"WM_SYSKEYDOWN","WM_SYSKEYUP",
"WM_SYSCHAR","WM_SYSDEADCHAR"};
TCHAR temp[50] = "";
int i;
switch (umsg)
{
case WM_CHAR:
hdc = GetDC(hwnd);
if (GetKeyState(VK_LCONTROL)<0)
{
sprintf(temp, "按下了左边的Ctrl键和%c键",wParam + 'A' - 1);
TextOut(hdc, 0, 0, temp, strlen(temp));
}
ReleaseDC(hwnd, hdc);
break;
/* case WM_KEYDOWN:
hdc = GetDC(hwnd);
if (wParam == VK_LEFT)
{
sprintf(temp, "按下了左方向键");
TextOut(hdc, 0, 0, temp, strlen(temp));
}
else if (wParam == VK_RIGHT)
{
sprintf(temp, "按下了右方向键");
TextOut(hdc, 0, 0, temp, strlen(temp));
}
else if (wParam == VK_UP)
{
sprintf(temp, "按下了上方向键");
TextOut(hdc, 0, 0, temp, strlen(temp));
}
else if (wParam == VK_DOWN)
{
sprintf(temp, "按下了下方向键");
TextOut(hdc, 0, 0, temp, strlen(temp));
}
ReleaseDC(hwnd, hdc);
case WM_KEYUP:
case WM_CHAR:
case WM_DEADCHAR:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_SYSCHAR:
case WM_SYSDEADCHAR:
if (cur >= 10)
{
for (i = 0;i < 9; i++)
{
strcpy(str[i], str[i + 1]);
}
cur = 9;
}
GetKeyNameText(lParam, temp, sizeof(temp)/sizeof(TCHAR));
sprintf(str[cur], "%d::%s::::::%d::::::%s", num + 1,
key_kode[umsg - WM_KEYFIRST], wParam,temp );
cur++;
num++;
hdc = GetDC(hwnd);
for (i = 0; i < cur; i++)
{
TextOut(hdc, 0, i * 20, str[i], strlen(str[i]));
}
ReleaseDC(hwnd, hdc);
break; */
case WM_DESTROY:
PostQuitMessage(0);
default:
return DefWindowProc(hwnd, umsg, wParam, lParam);
break;
}
return 0;
}