我正在尝试禁止任务切换键(例如 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 组合感到茫然。 有什么建议?感谢。
答案 0 :(得分:1)
这应该可以解决问题:
bool ControlDown = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
if (lParam.Key == VK_ESCAPE && ControlDown)
suppress = true;
答案 1 :(得分: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);
}