[英]how can I disable windows key in c#?
如何禁用或鎖定Windows按鈕?
使用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上找到相當完整的解釋和演練。 這是從該示例執行所有操作的自包含類文件的直接鏈接 (如果您使用WPF將其編譯為干凈,則需要您手動引用System.Windows.Forms dll或僅更改'System.Windows。 Forms.Keys對System.Windows.Input.Key的引用應該有效。
記得調用UnhookWindowsHookEx()(該類在Dispose()中執行此操作)以取消捕獲您的捕獲,否則人們會討厭您。
你需要一個鍵盤鈎。 從這樣的地方開始:
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
/// <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();
}
}
}
假設您希望永久禁用Windows密鑰,而不僅僅是在代碼處於焦點時,那么您可以通過編輯注冊表來執行此操作,如下所示:
要禁用 :將名為“ Scancode Map ”的新REG_BINARY值添加到“ HKEY_LOCAL_ MACHINE \\ System \\ CurrentControlSet \\ Control \\ Keyboard Layout ”,數據值為“ 00000000000000000300000000005BE000005CE000000000 ”
要啟用 :完全從注冊表中刪除“ 掃描碼映射 ”值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.