低层键盘钩子


//
// The section is SHARED among all instances of this DLL.
// A low-level keyboard hook is always a system-wide hook.
//
#pragma data_seg (".mydata")
HHOOK g_hHookKbdLL = NULL;
BOOL  g_bBeep = FALSE; 
#pragma data_seg ()
#pragma comment(linker, "/SECTION:.mydata,RWS") // tell linker: make it  shared

//
// Low-level keyboard hook:
// Trap task-switching keys by returning without passing along.
//
LRESULT CALLBACK MyTaskKeyHookLL(int nCode, WPARAM wp, LPARAM lp)
{
   KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT *) lp;

   if (nCode==HC_ACTION) {
      BOOL bCtrlKeyDown = GetAsyncKeyState(VK_CONTROL)>>((sizeof(SHORT) * 8) - 1);

      if ((pkh->vkCode == VK_ESCAPE && bCtrlKeyDown)  ||  //        
          (pkh->vkCode == VK_TAB && pkh->flags & LLKHF_ALTDOWN) ||   
          (pkh->vkCode==VK_ESCAPE && pkh->flags & LLKHF_ALTDOWN)||
          (pkh->vkCode==VK_LWIN || pkh->vkCode==VK_RWIN)) { 
         if (g_bBeep && (wp==WM_SYSKEYDOWN||wp==WM_KEYDOWN))
            MessageBeep(0); // only beep on downstroke if requested
         return 1; // gobble it: go directly to jail, do not pass go
      }
   }
   return CallNextHookEx(g_hHookKbdLL, nCode, wp, lp);
}

你可能感兴趣的:(键盘)