views:

385

answers:

3

I am trying to hook the keyboard in my program, but there is something that I can't accomplish. The method below is the most important part in my class where I handle certain key combinations. All of them work, but I also want to hook Ctrl-Alt-Tab. I've spent hours trying to figure out what to do, but I came empty handed. How can I hook this combination as well?

More Information can be found here:
http://msdn.microsoft.com/en-us/library/ms644967(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms927178.aspx

 private static IntPtr KeyboardHookHandler(int nCode, IntPtr wParam, KBDLLHookStruct lParam)
 {
   if (nCode == 0)
   {              

    if ( ( (lParam.flags == 32)  && (lParam.vkCode == 0x09) ) ||      // Alt+Tab
         ( (lParam.flags == 32)  && (lParam.vkCode == 0x1B) ) ||      // Alt+Esc
         ( (lParam.flags == 0 )  && (lParam.vkCode == 0x1B) ) ||      // Ctrl+Esc
         ( (lParam.flags == 1 )  && (lParam.vkCode == 0x5B) ) ||      // Left Windows Key
         ( (lParam.flags == 1 )  && (lParam.vkCode == 0x5C) ) ||      // Right Windows Key
         ( (lParam.flags == 32)  && (lParam.vkCode == 0x73) ) ||      // Alt+F4              
         ( (lParam.flags == 32)  && (lParam.vkCode == 0x20) ))        // Alt+Space

    {
        return new IntPtr(1);
    }
  }

  return CallNextHookEx(hookPtr, nCode, wParam, lParam);
}
A: 

It might be worth your while to check out this article by Paul DiLascia who shows how to trap the keys Ctrl+Alt+Del combination here. There is a version available for the .NET framework found on CodeProject here and here.

Hope this helps, Best regards, Tom.

tommieb75
A: 

You should subclass the win32 message pump.
Maybe you will get some ideas from this VC6 project Trap CtrlAltDel; Hide Application in Task List on Win2000/XP

Kb
A: 

Worlds, you are trapping the keys correctly but you need to perform bitwise AND operations on your lParam.flags to determine whether more than one modifier key was pressed.

This is off the top of my head but i think the code that looks like this:

(lParam.flags == 32)

should look something like:

((lParam.flags & 32 == 32) && (lParam.flags & 16 == 16))

32 and 16 are arbitrary in this example. You need to figure out what values ALT and CTRL actually are. They will be 1, 2, 4 ... 16, 32 etc. so that they can be OR'ed together into a single value.

Paul Sasik
LCTRL has flag 0 when it is pressed and 128 when released. RCTRL has flag 1 when pressed and 129 when released. The system does not give me the sum of three flags corresponding to the keys. I can only trap two keys at most.