有人知道如何在C#中禁用键盘全局开发钩子?
我有一个源代码,它实现了一个钩子,可以在同一个应用程序中禁用键盘,但是如果我执行它并且键盘在文本编辑器中工作,那么在Windows 7中不能工作(我可以继续写)我希望全局捕获/禁用键盘(对于所有正在运行的应用程序)。我的目的是完全锁定Windows 7。
这是我的钩码:
globalKeyboardHook gkh = new globalKeyboardHook();
/// <summary>
/// Installs the global hook
/// </summary>
public void hook() {
IntPtr hInstance = LoadLibrary("User32");
hhook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, hInstance, 0);
}
public int hookProc(int code, int wParam, ref keyboardHookStruct lParam) {
//indicates if any of underlaing events set e.Handled flag
bool handled = false;
IntPtr lFakeParam = new IntPtr(lParam.vkCode);
if ((code >= 0) && (KeyDown != null || KeyUp != null || wParam == WM_SYSKEYDOWN || wParam == WM_SYSKEYUP))
{
//read structure KeyboardHookStruct at lParam
keyboardHookStruct MyKeyboardHookStruct = (keyboardHookStruct)Marshal.PtrToStructure(lFakeParam, typeof(keyboardHookStruct));
Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
KeyEventArgs e = new KeyEventArgs(keyData);
log.Debug("cached wParam = [" + wParam + "] vkCode = [" + MyKeyboardHookStruct.vkCode + "] flags = [" + MyKeyboardHookStruct.flags + "]");
if (MyKeyboardHookStruct.vkCode == 144)
{
// NUMLOCK
handled = false;
}
KeyDown(this, e);
}
//if event handled in application do not handoff to other listeners
if (handled)
{
SendKeys.Send("{NUMLOCK}");
SendKeys.Send("{NUMLOCK}");
return 1;
//return CallNextHookEx(hKeyboardHook, nCode, wParam, lFakeParam);
}
else
{
return CallNextHookEx(hhook, code, wParam, ref lParam);
}
}
提前致谢!