📄 keytrac.c
字号:
//======================================================================
//功能描述:
// 每次按键时,在窗口输出按键信息,包括按键产生的消息、消息的参数、控制键的使用等
//实现方法:
// 在窗口收到键盘消息后,记录键击的信息,强制窗体重绘
// 在窗体重绘时,输出键击的信息
//======================================================================
#include <windows.h> // For all that Windows stuff
#include <commctrl.h> // Command bar includes
#include "keytrac.h" // Program-specific stuff
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT("KeyTrac");
HINSTANCE hInst; // Program instance handle
KPRESSINFO kinfo[24];
int nKeyCnt=0;
int nFontHeight=0;
// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
WM_CREATE, DoCreateMain,
WM_KEYDOWN,DoKeysMain,
WM_KEYUP,DoKeysMain,
WM_CHAR,DoKeysMain,
WM_SYSKEYDOWN,DoKeysMain,
WM_SYSKEYUP,DoKeysMain,
WM_SYSCHAR,DoKeysMain,
WM_DEADCHAR,DoKeysMain,
WM_SYSDEADCHAR,DoKeysMain,
WM_PAINT, DoPaintMain,
WM_DESTROY, DoDestroyMain,
};
//======================================================================
//
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
MSG msg;
int rc = 0;
HWND hwndMain;
// Initialize application.
rc = InitApp (hInstance);
if (rc) return rc;
// Initialize this instance.
hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0)
return 0x10;
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitApp - Application initialization
//
int InitApp (HINSTANCE hInstance) {
WNDCLASS wc;
// Register application main window class.
wc.style = 0; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = 0; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL, // Application icon
wc.hCursor = NULL; // Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass(&wc) == 0) return 1;
return 0;
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) {
HWND hWnd;
// Save program instance handle in global variable.
hInst = hInstance;
// Create main window.
hWnd = CreateWindow (szAppName, // Window class
TEXT("KeyTrac"), // Window title
WS_VISIBLE, // Style flags
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
CW_USEDEFAULT, // Initial Width
CW_USEDEFAULT, // Initial Height
NULL, // Parent
NULL, // Menu, must be null
hInstance, // App instance
NULL); // Pointer to create
// parameters
// Return fail code if window not created.
if (!IsWindow (hWnd)) return 0;
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
return nDefRC;
}
//======================================================================
// Message handling procedures for MainWindow
//
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
INT i;
//
// Search message list to see if we need to handle this
// message. If in list, call procedure.
//
for (i = 0; i < dim(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.
//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
HDC hdc;
HWND hwndCB;
TEXTMETRIC tm;
// Create a command bar.
hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR);
// Add exit button to command bar.
CommandBar_AddAdornments (hwndCB, 0, 0);
hdc=GetDC(hWnd);
GetTextMetrics(hdc,&tm);
nFontHeight=tm.tmHeight+tm.tmExternalLeading;
ReleaseDC(hWnd,hdc);
return 0;
}
//----------------------------------------------------------------------
// DoPaintMain - Process WM_PAINT message for window.
//
LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
/********************输出键击信息********************/
PAINTSTRUCT ps;
HDC hdc=BeginPaint(hWnd,&ps);
TCHAR szTextout[256]=TEXT("");
RECT rect;
RECT rectcli;
int i;
GetClientRect(hWnd,&rect);
//绘图区域rect
rect.top+=CommandBar_Height(GetDlgItem(hWnd,IDC_CMDBAR));
//最底端一行rectcli
rectcli=rect;
rectcli.top=rectcli.bottom-nFontHeight;
if(nKeyCnt>0)
{
for(i=0;i<nKeyCnt;i++)
{
ScrollDC(hdc,0,-nFontHeight,&rect,&rect,NULL,NULL);
wsprintf(szTextout,TEXT("key:%s wParam:%08x lParam:%08x other:"),kinfo[i].szMsgTxt,kinfo[i].wParam,kinfo[i].lParam);
if (kinfo[i].wFlag & FLAG_LCONTROL)
lstrcat(szTextout,TEXT("LC"));
if (kinfo[i].wFlag & FLAG_RCONTROL)
lstrcat(szTextout,TEXT("RC"));
if (kinfo[i].wFlag & FLAG_LSHIFT)
lstrcat(szTextout,TEXT("LS"));
if (kinfo[i].wFlag & FLAG_RSHIFT)
lstrcat(szTextout,TEXT("RS"));
if (kinfo[i].wFlag & FLAG_LMENU)
lstrcat(szTextout,TEXT("LA"));
if (kinfo[i].wFlag & FLAG_RMENU)
lstrcat(szTextout,TEXT("RA"));
//在指定区域(retcli)的指定位置(5,rect.bottom-nFontHeight),
//输出指定长度(lstrlen(szTextout))的字符串(szTextout)
ExtTextOut(hdc,5,rect.bottom-nFontHeight,ETO_OPAQUE,&rectcli,szTextout,lstrlen(szTextout),NULL);
}
nKeyCnt=0;
}
EndPaint(hWnd,&ps);
return 0;
}
LRESULT DoKeysMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
/*******************记录键击的信息**********************/
//消息编号
kinfo[nKeyCnt].wMsg=wMsg;
//wParam
kinfo[nKeyCnt].wParam=wParam;
//lParam
kinfo[nKeyCnt].lParam=lParam;
//提示信息
switch(wMsg)
{
case WM_KEYDOWN:
lstrcpy(kinfo[nKeyCnt].szMsgTxt,TEXT("WM_KEYDOWN"));
break;
case WM_SYSKEYDOWN:
lstrcpy(kinfo[nKeyCnt].szMsgTxt,TEXT("WM_SYSKEYDOWN"));
break;
case WM_CHAR:
lstrcpy(kinfo[nKeyCnt].szMsgTxt,TEXT("WM_CHAR"));
break;
case WM_SYSCHAR:
lstrcpy(kinfo[nKeyCnt].szMsgTxt,TEXT("WM_SYSCHAR"));
break;
case WM_DEADCHAR:
lstrcpy(kinfo[nKeyCnt].szMsgTxt,TEXT("WM_DEADCHAR"));
break;
case WM_SYSDEADCHAR:
lstrcpy(kinfo[nKeyCnt].szMsgTxt,TEXT("WM_SYSDEADCHAR"));
break;
case WM_KEYUP:
lstrcpy(kinfo[nKeyCnt].szMsgTxt,TEXT("WM_KEYUP"));
break;
case WM_SYSKEYUP:
lstrcpy(kinfo[nKeyCnt].szMsgTxt,TEXT("WM_SYSKEYUP"));
break;
default:
lstrcpy(kinfo[nKeyCnt].szMsgTxt,TEXT("UNKNOWN"));
break;
}
//换档键标示
kinfo[nKeyCnt].wFlag=0;
if (GetKeyState(VK_LCONTROL))
kinfo[nKeyCnt].wFlag|=FLAG_LCONTROL;
if (GetKeyState(VK_RCONTROL))
kinfo[nKeyCnt].wFlag|=FLAG_RCONTROL;
if (GetKeyState(VK_LSHIFT))
kinfo[nKeyCnt].wFlag|=FLAG_LSHIFT;
if (GetKeyState(VK_RSHIFT))
kinfo[nKeyCnt].wFlag|=FLAG_RSHIFT;
if (GetKeyState(VK_LMENU))
kinfo[nKeyCnt].wFlag|=FLAG_LMENU;
if (GetKeyState(VK_RMENU))
kinfo[nKeyCnt].wFlag|=FLAG_RMENU;
nKeyCnt++;
/*********************强制窗口重绘********************/
InvalidateRect(hWnd,NULL,FALSE);
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -