Home > Windows API > Setting up a low level keyboard hook using WH_KEYBOARD_LL

Setting up a low level keyboard hook using WH_KEYBOARD_LL

How to disable Alt + Esc, Alt + Tab, Ctrl + Esc…

// Here is a small program by Jeffrey Richter...
// Written 2000 Jeffrey Richter
LRESULT CALLBACK LowLevelKeyboardProc( int nCode,
                                       WPARAM wParam,
                                       LPARAM lParam)
{

  BOOL fEatKeystroke = FALSE;
  if (nCode == HC_ACTION)
  {
     switch (wParam)
     {
        case WM_KEYDOWN:
        case WM_SYSKEYDOWN:
        case WM_KEYUP:
        case WM_SYSKEYUP: 

           // Get hook struct
           PKBDLLHOOKSTRUCT p = ( PKBDLLHOOKSTRUCT ) lParam;
           fEatKeystroke = (( p->vkCode == VK_TAB ) &&
                           (( p->flags & LLKHF_ALTDOWN ) != 0 )) ||
                           (( p->vkCode == VK_ESCAPE ) &&
                           (( p->flags & LLKHF_ALTDOWN ) != 0 )) ||
                           (( p->vkCode == VK_ESCAPE ) &&
                           (( GetKeyState( VK_CONTROL ) & 0x8000) != 0 )); 

           // Check whether we have a key
           if( fEatKeystroke )
           {
              MessageBox( 0, "Eating key stroke", "Error", MB_OK );
              break;
           }
     }// End switch
  }// End if 

   // Did we trap a key??
   return( fEatKeystroke ? 1 : CallNextHookEx( NULL, nCode, wParam, lParam ));
}// End LowLevelKeyboardProc 

// WinMain function
int WINAPI WinMain( HINSTANCE hinstExe, 
                    HINSTANCE,
                    PTSTR pszCmdLine,
                    int )
{
// Install the low-level keyboard & mouse hooks
   HHOOK hhkLowLevelKybd  = SetWindowsHookEx( WH_KEYBOARD_LL,
                                              LowLevelKeyboardProc,
                                              hinstExe,
                                              0 ); 

   // Keep this app running until we're told to stop
   MessageBox( NULL,
               TEXT( "Click \"Ok\" to re-enable these keys." ),
               TEXT( "Disable Low-Level Keys" ),
               MB_OK ); 

   // Release hook
   UnhookWindowsHookEx(hhkLowLevelKybd); 

   return(0);
}// End winmain
Advertisements
  1. Rakesh Choudhary
    March 31, 2009 at 10:21 am

    Hi frndz,
    I have made a win32 application in which i m getting input from keyboard as well as a barcode scanner. I want my application to process only keystrokes coming from the keyboard, but as you all may know barcode scanner puts it’s input in keyboard buffer and for application both are coming from keyboard.So i m not able to do that. Then i came to know about low level keyboard hook.I m using low level keyboard hook in which i m expecting that if input is coming from barcode then it would surely be injected in keyboard buffer somehow. So the KBDLLHOOKSTRUCT structure should have LLKHF_INJECTED on. But that is not the case here. it is coming off even if input is coming from barcode scanner. Is my assumption wrong..? Is there any other way…?
    Please Help……
    Regards,
    Rakesh.

  2. February 13, 2009 at 11:40 am

    hi frz
    i use this code in my application but it give me 2 same character
    ex
    if i press ‘A’ then it return me ‘A”A’ similar for all character how can i stop it i modified some what code like this
    actually i want to collect only character press between ‘|'(my first character) and ‘~’ (this is my last char)

    if (nCode == HC_ACTION)
    {
    switch (wParam)
    {
    case WM_KEYDOWN:
    case WM_SYSKEYDOWN:
    case WM_KEYUP:
    case WM_SYSKEYUP:

    // Get hook struct
    PKBDLLHOOKSTRUCT p = ( PKBDLLHOOKSTRUCT ) lParam;

    if( (GetKeyState( VK_SHIFT ) & 0x8000) != 0)
    {

    switch(p->vkCode)
    {
    case VK_OEM_3:
    strScan +=”~”;
    //m_StrScanValue =strPreTranslateScan;
    //OnEnChangeScanvalue();
    MessageBox( 0, strScan, “3”, MB_OK );

    break;
    case VK_OEM_5:
    //ShowWindow(this,WM_SHOWWINDOW);
    //CInstallhookDlg *TOPSBSdlg = new CInstallhookDlg();
    // ClearAlldata();
    strScan =”|”;
    MessageBox( 0, strScan, “5”, MB_OK );
    break;
    }

    }
    else
    {
    CString cs;
    cs.Format(“%c”,p->vkCode);
    strScan +=cs;
    MessageBox( 0, strScan, “else”, MB_OK );

    }

    }// End switch
    }// End if

    // Did we trap a key??
    return( fEatKeystroke ? 1 : CallNextHookEx( NULL, nCode, wParam, lParam ));

  3. Ricardo Martinez
    December 27, 2008 at 2:14 am

    Hello
    Alex for disable ctrl+r, ctrl+e, etc… you can try a register tweak then a command to explorer load the change, tweakui have a option for disable win+X combinations…

  4. October 28, 2008 at 8:01 am

    Thanks for your comments.

    I think you can devise your own methods to disable these key combinations but I would personally feel annoyed if an application does this.

    Well you might have your own reasons for doing so. 😉

    • Popescu Radu Alexandru (aka u0m3)
      April 13, 2009 at 4:05 am

      I said ctr+e, ctr+r… Sorry for the mistake… I was thinking of VK_RWIN or VK_LWIN instead. I find this especially tricky to hook, when used in a combination. For example if you want to lunch some other program than explorer.exe with win+e, you have to catch it when it is pressed and when it is released… But if the user presses win+r for example… You can get in a bit of a mess…

      I have been trying to fix this for months… and I still can’t figure it out.

      Thank you in advance for any suggestions.

  5. Popescu Radu Alexandru (aka u0m3)
    October 12, 2008 at 5:37 pm

    Hi.
    Great tutorial. I was wondering if you could show an example to “disable” (change default behavior) for other key combinations (ex: ctrl+r, ctrl+e, so on and so forth).

    Thanks in advance.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: