如何禁用或锁定Windows按钮?
答案 0 :(得分:4)
你需要一个键盘钩子。从这样的地方开始:
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
答案 1 :(得分:4)
/// <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();
}
}
}
答案 2 :(得分:4)
使用Windows钩子比修改注册表要简洁得多。此外,有时人们已经设置了自己的个性化扫描码地图,并且覆盖它们并不是一件好事。
要使用Windows键挂钩功能,您需要DllImport一对winapi函数:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
[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找到相当完整的解释和演练。这是一个direct link来自该示例的自包含类文件,它执行所有操作(如果您使用WPF将其编译为干净,则需要您手动引用System.Windows.Forms dll或仅更改'System .Windows.Forms.Keys对System.Windows.Input.Key的引用应该有效。
请记得调用UnhookWindowsHookEx()(该类在Dispose()中执行此操作)以取消捕获您的捕获,否则人们会讨厌您。
答案 3 :(得分:1)
假设您希望永久禁用Windows密钥,而不仅仅是在代码处于焦点时,那么您可以通过编辑注册表来执行此操作,如下所示:
要停用: 将名为“扫描码地图”的新 REG_BINARY 值添加到“ HKEY_LOCAL_ MACHINE \ System \ CurrentControlSet \ Control \ Keyboard Layout ”,数据值为“的 00000000000000000300000000005BE000005CE000000000 强>”
启用: 完全从注册表中删除“扫描码映射”值。