我也正好在研究钩子函数,大家研究研究
[email protected]

解决方案 »

  1.   

    我也在研究这个呢,发给我看看
    [email protected]
      

  2.   

    [email protected]
    一起学习,多多交流
      

  3.   

    我看了你的程序了,知道是什么地方出问题了。
    因为WH_KEYBOARD钩子要截获WM_KEYDOWN和WM_KEYUP,当F10被按下时,你的钩子函数被执行,AfxMessageBox了一下;F10弹起你的钩子函数又被执行,又AfxMessageBox了一下(这一下是不必要的)。程序改一下就行了。 if (((DWORD)lParam & 0x40000000)//判断是按下
     && (HC_ACTION == nCode))
     { 
      switch (wParam)
      {
       case VK_F10:
       //AfxMessageBox("Caught the F10 key!");
        bHandledKeystroke = TRUE;
    str.Format(_T("bHandledKeystroke%d"),
    bHandledKeystroke);
    AfxMessageBox(str);
       break;   default :
       break ;
      }
     }
    //str.Format(_T("bHandledKeystroke%d"),
    // bHandledKeystroke);
    //AfxMessageBox(str);        这里移到上面去了,F10弹起这里也执行。
      

  4.   

    WM_KEYDOWN使bHandledKeystroke为1,而WM_KEYUP使bHandledKeystroke为0。
         可是有一个问题,在这个函数(KbdHookProc)的最后还有一句:
    return (bHandledKeystroke ?  TRUE : ::CallNextHookEx (ghKeyHook, nCode, wParam, lParam)); 
         它将视 bHandledKeystroke的值而做,我想要它做的是:当F10按下后,bHandledKeystroke为TRUE,
    然后钩子函数返回真,F10的功能失去;当按下其他键时,bHandledKeystroke为FALSE,则调用
    ::CallNextHookEx (ghKeyHook, nCode, wParam, lParam),不让其他键失去作用。
         可是现在的问题是当出了SWITCH循环后,由于WM_KEYUP的作用使bHandledKeystroke最终为0,这使得
    F10的功能并没有失去,如在VC环境下,F10仍然能调试。所以我想应该单独处理WM_KEYDOWN和WM_KEYUP消息,
    可是该怎么做呢?请指教!
      

  5.   

    VC中的F10功能键捕捉的是WM_KEYDOWN消息,而你的程序捕捉的是WM_KEYUP,那当然过滤不去了。
    你应注意到,你的程序中有这个逻辑关系((DWORD)lParam & 0x40000000)),其意义就是当lParam的最高位为1时条件成立,即按键释放之时,具体你可以参阅MSDN。
    将程序改成如下,便可以过滤掉VC中的F10功能键:
    LRESULT EXPORTED_DLL_FUNCTION KbdHookProc 
    (int nCode, WPARAM  wParam, LPARAM lParam)
    {CString str;
     BOOL bHandledKeystroke = FALSE; if (HC_ACTION == nCode)
     { 
      switch (wParam)
      {
       case VK_F10:
       if(!((DWORD)lParam & 0x40000000))
       {

     bHandledKeystroke = TRUE;
    str.Format(_T("Caught the F10 key!\nbHandledKeystroke%d"), bHandledKeystroke);
    AfxMessageBox(str);
       }   break;   default :
       break ;
      }
     }
     return (bHandledKeystroke ? 
      TRUE : 
      ::CallNextHookEx (ghKeyHook, nCode, wParam, lParam));
    }