如下:一个利用钩子取键盘按键的函数
LRESULT CALLBACK KeyboardProc(int iCode,WPARAM wParam,LPARAM lParam)
{
char cCh[1];
cCh[0]=wParam;
if(iCode==HC_ACTION) 

if(lParam & 0x80000000) 
{
cCh[0]=wParam;
CFile OutFile;
OutFile.Open("C:\\KeyLog.Log",CFile::modeWrite);
OutFile.SeekToEnd();
OutFile.Write(cCh,1);
OutFile.Close(); 


LRESULT lrRetVal=CallNextHookEx(Hook,iCode,wParam,lParam);
return lrRetVal;
}为什么KeyLog.Log中总为1个字节?钩子安装是对的。
还有,怎么从wparam和lparam中取得按键信息?

解决方案 »

  1.   

    应该没有问题,建议打开的函数中第二个参数这样写:
    CFile::modeWrite|CFile::modeRead
      

  2.   

    MSDN:wParam 
    Specifies the virtual-key code of the key that generated the keystroke message. 
    lParam 
    Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag. This parameter can be a combination of the following values: Value Description 
    0–15 Specifies the repeat count. The value is the number of times the keystroke is repeated as a result of the user's holding down the key. 
    16–23 Specifies the scan code. The value depends on the original equipment manufacturer (OEM). 
    24 Specifies whether the key is an extended key, such as a function key or a key on the numeric keypad. The value is 1 if the key is an extended key; otherwise, it is 0. 
    25–28 Reserved. 
    29 Specifies the context code. The value is 1 if the alt key is down; otherwise, it is 0. 
    30 Specifies the previous key state. The value is 1 if the key is down before the message is sent; it is 0 if the key is up. 
    31 Specifies the transition state. The value is 0 if the key is being pressed and 1 if it is being released. 
    不知楼主想提取什么特殊信息?
      

  3.   

    我验证过你的程序,没有问题,完全正常,我想你的钩子安装有问题,我的钩子安装如下:
    1)做一个dll,钩子处理函数如下:
    void CALLBACK KeyboardProc(int iCode,WPARAM wParam,LPARAM lParam)
    {
    char cCh[1];
    cCh[0]=wParam;
    if(iCode==HC_ACTION) 

    if(lParam & 0x80000000) 
    {
    cCh[0]=wParam;
    CFile OutFile;
    OutFile.Open("C:\\KeyLog.Log",CFile::modeWrite);
    OutFile.SeekToEnd();
    OutFile.Write(cCh,1);
    OutFile.Close(); 


    }
    2)钩子安装如下:供参考
    HOOKPROC hkprc; 
    static HINSTANCE hinstDLL; 
    static HHOOK hhookSysMsg; 
    hinstDLL = LoadLibrary((LPCTSTR) ".\\mydll.dll"); 
    hkprc = (HOOKPROC)GetProcAddress(hinstDLL, "KeyboardProc"); 
    hhookSysMsg = SetWindowsHookEx(WH_KEYBOARD,hkprc,hinstDLL,0); //keyboard for example结果是:按下的键被记录在 keylog.log中。