2

我正在尝试通过使用低级键盘挂钩来抑制任务切换键(例如winkey, alt+ tab, alt+ esc, ctrl+等)。esc

我正在使用以下LowLevelKeyboardProc回调:

IntPtr HookCallback(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam) 
{
    if (nCode >= 0)
    {
            bool suppress = false;

            // Suppress left and right windows keys.
            if (lParam.Key == VK_LWIN || lParam.Key == VK_RWIN) 
                suppress = true;

            // Suppress alt-tab.
            if (lParam.Key == VK_TAB && HasAltModifier(lParam.Flags)) 
                suppress = true;

            // Suppress alt-escape.
            if (lParam.Key == VK_ESCAPE && HasAltModifier(lParam.Flags)) 
                suppress = true;

            // Suppress ctrl-escape.
            /* How do I hook CTRL-ESCAPE ? */

            // Suppress keys by returning 1.
            if (suppress) 
                return new IntPtr(1);
    }
    return CallNextHookEx(HookID, nCode, wParam, ref lParam);
}

bool HasAltModifier(int flags)
{
    return (flags & 0x20) == 0x20;
}

但是,我不知道如何抑制CTRL+ESC组合。有什么建议么?谢谢。

4

2 回答 2

1

这应该可以解决问题:

bool ControlDown = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
if (lParam.Key == VK_ESCAPE && ControlDown)
    suppress = true;
于 2010-04-09T01:42:47.233 回答
0

这就是我要做的工作,我还添加了 Alt+F4 以防止应用程序关闭。

    private static bool lastWasCtrlKey = false;

    private static IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
    {
        if (nCode >= 0)
        {
            KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));

            // Disabling Windows keys 
            switch (objKeyInfo.key)
            {
                case Keys.RWin:
                case Keys.LWin:
                case Keys.Tab when HasAltModifier(objKeyInfo.flags):
                case Keys.Escape when HasAltModifier(objKeyInfo.flags):
                case Keys.Delete when HasAltModifier(objKeyInfo.flags):
                case Keys.F4 when HasAltModifier(objKeyInfo.flags):
                case Keys.Escape when lastWasCtrlKey:
                    lastWasCtrlKey = false;
                    return (IntPtr)1; 
                case Keys.LControlKey:
                case Keys.RControlKey:
                    lastWasCtrlKey = true;
                    break;
                case Keys.LShiftKey:
                case Keys.RShiftKey:
                    // Do nothing as the Ctrl key could have been before this
                    break;
                default:
                    lastWasCtrlKey = false;
                    break;
            }
        }
        return CallNextHookEx(ptrHook, nCode, wp, lp);
    }
于 2019-01-08T16:01:06.980 回答