为了一个键盘钩子,提问了N次没人理,查资料N*N*N*N*N*N次找不到。搞了五天,完全白忙,就要疯啦一见到电脑就烦,
真想砸了 扔到坛上
大家快来呀,高手救命,非高手看热闷,看完拿一块显卡,硬盘马达,显视屏碴子跑.....不满意扔回来我
俺不干了唉

解决方案 »

  1.   

    全局键盘钩子只想钩取 “按下(WM_KEYDOWN)” 动作 及长按住一个键时一连串的WM_KEYDOWN, 就这么简单*&^*&%&^$(*&^*&LRESULT CALLBACK KeyBoardProc(int nCode,WPARAM wParam, LPARAM lParam )
    {
    if(nCode>=0)
    {
    if((lParam&0xc000ffff)==1) //aaaaaaaaaaaaaaaaa
        ::PostMessage(m_h0Main,WM_HOOK_KEYDOWN,wParam,lParam); }
    return CallNextHookEx(hKeyHook,nCode,wParam,lParam);
    }
    其中aaaaaaaaaaaaa分别换成了:
    lParam &0x80000000 lParam & 0x40000000 lParam & 0xA0000000  lParam & 0xc000ffff不管那个,运行起来有时很正常,有时却会:莫名奇妙地,按一次键却钩到两个“按下”特别是按得快的话,一团糟
    位操作搞烦了,再来用位段替换掉 lParam 
    如下:struct Lparam_Bit_Struct
    {
    unsigned Repeat_Code            :16;   //0~15 位
        unsigned Scan_Code              :8;         //16~23
        unsigned IsExtemded_Key         :1;         // 24
        unsigned Reserve_Data           :4;         // 25~28
        unsigned IsALT_Down             :1;         // 29
        unsigned Pre_Key_State          :1;         // 30
        unsigned This_Key_State         :1;         // 31
    };Lparam_Bit_Struct *LparamBStruct;
    LparamBStruct=(Lparam_Bit_Struct *)(&lParam);if(LparamBStruct->This_Key_State==0)
    {
    ::PostMessage(m_h0Main,WM_HOOK_KEYDOWN,wParam,lParam);
    }一样!!!—*……*%狂晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕晕呀,我砸电脑,靠lParam   里面究境放了什么东西&^*%&^$^%$^&参看:
        lParam
        击键信息产生多个标志的组合值,包括重复击键次数, 键盘扫描代码, 功能键标志位等:
        0–15 位:重复击键次数;
        16–23位:键盘扫描代码;
        24    位:功能键或小键盘标志位,1为有效;
        29    位:alt键是否按下,1为按下;
        30    位:前一次击键的状态,1为按下,0为放开
        31    位:本次击键状态,0为正在按下,1为正在放开msdn e 文原解:
    lParam 
    [in] Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag. This parameter can be one or more 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. 
      

  2.   

    呵呵今天灵感一来
    问题解决LRESULT CALLBACK KeyBoardProc(int nCode,WPARAM wParam, LPARAM lParam )
    {
    if(nCode>=0)    //////   这里
    {
    if((lParam&0xc000ffff)==1) //aaaaaaaaaaaaaaaaa
        ::PostMessage(m_h0Main,WM_HOOK_KEYDOWN,wParam,lParam); }
    return CallNextHookEx(hKeyHook,nCode,wParam,lParam);
    }LRESULT CALLBACK KeyBoardProc(int nCode,WPARAM wParam, LPARAM lParam )
    {
    if(nCode==HC_ACTION)        // 改                (宏 HC_ACTION==0)
    {
    if((lParam&0xc000ffff)==1) //aaaaaaaaaaaaaaaaa
        ::PostMessage(m_h0Main,WM_HOOK_KEYDOWN,wParam,lParam); }
    return CallNextHookEx(hKeyHook,nCode,wParam,lParam);
    }
    我看到很多的键盘钩子例子都是写成  if(nCode>=0) 误人子弟呀如果用 if(nCode>=0)  的话,若按键速度很快,你可能会收到两个KEYDOWN  与 KEYUP  特别是键盘钩子DLL 所注入的进程反应太慢的话
    好像原理是这样的,当 nCode==HC_NOREMOVE (宏HC_NOREMOVE==3) 时,if(nCode>=0) 为真,即我的程序收到KEYDOWN,而实际上,nCode==HC_NOREMOVE 表示 上一次按键消息还在系统队列里.