我怎样才能禁用C#中的Windows键?

我怎样才能禁用或locking窗口button

/// <summary> /// Security routines related to the Windows Key on a standard personal computer Keyboard /// </summary> public static class WindowsKey { /// <summary> /// Disables the Windows Key /// </summary> /// <remarks>May require the current user to logoff or restart the system</remarks> public static void Disable() { RegistryKey key = null; try { key = Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Control\\Keyboard Layout", true); byte[] binary = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5B, 0xE0, 0x00, 0x00, 0x5C, 0xE0, 0x00, 0x00, 0x00, 0x00 }; key.SetValue("Scancode Map", binary, RegistryValueKind.Binary); } catch (System.Exception ex) { Debug.Assert(false, ex.ToString()); } finally { key.Close(); } } /// <summary> /// Enables the Windows Key /// </summary> /// <remarks>May require the current user to logoff or restart the system</remarks> public static void Enable() { RegistryKey key = null; try { key = Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Control\\Keyboard Layout", true); key.DeleteValue("Scancode Map", true); } catch (System.Exception ex) { Debug.Assert(false, ex.ToString()); } finally { key.Close(); } } } 

你需要一个键盘钩子。 开始这样的地方:

  hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hInstance, 0); 

并继续像这样:

  LRESULT KeyboardProc(...) { if (Key == VK_SOMEKEY) return 1; // Trap key return CallNextHookEx(...); // Let the OS handle it } 

有关更多详细信息: http : //www.codeproject.com/KB/winsdk/AntonioWinLock.aspx

使用窗口钩子比修改注册表要干净得多。 另外,有时候人们会自己设置个性化的扫描码地图,覆盖它们不是一件很好的事情。

要使用Windows的键钩函数,你需要DllImport一对winapi函数:

 [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern IntPtr GetmoduleeHandle(string lpmoduleeName); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern IntPtr SetWindowsHookEx(int idHook, HookHandlerDelegate lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] public static extern short GetKeyState(int keyCode); 

可以在CodeProject上找到一个相当完整的解释和演练。 这里是直接链接到一个自包含的类文件从这个例子,一切(要得到它编译干净,如果您使用WPF将需要您手动引用System.Windows.Forms DLL或只是更改'System.Windows。 Forms.Keys对System.Windows.Input.Key的引用应该可以)。

记得调用UnhookWindowsHookEx()(类在Dispose()中做到这一点)来解除你的捕获,否则人们会恨你。

假设你希望永久地禁用Windows密钥,而不是仅仅当你的代码处于焦点状态,那么你可以通过编辑注册表来完成,如下所示:

禁用 :将数据值为“ 00000000000000000300000000005BE000005CE000000000 ”的名为“ Scancode Map ”的新REG_BINARY值添加到“ HKEY_LOCAL_ MACHINE \ System \ CurrentControlSet \ Control \ Keyboard Layout

启用 :从注册表中完全删除“ Scancode Map ”值。