Reputation: 3009
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);
}
Upvotes: 3
Views: 6564
Reputation: 81429
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.
Upvotes: 2
Reputation: 7400
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
Upvotes: 0