简体   繁体   中英

How can I hook Ctrl-Alt-Tab?

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);
}

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM