如何在 c# 中禁用 windows 键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3361109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:55:14  来源:igfitidea点击:

how can I disable windows key in c#?

c#windowsbutton

提问by nectar

How can i disable or lock windows button?

如何禁用或锁定 Windows 按钮?

采纳答案by David Anderson

    /// <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();
            }
        }
    }

回答by Sogger

Using the windows hooks is a lot cleaner than modifying the registry. Additionally, sometimes people have setup personalized scancode maps of their own, and overwriting them is not a very kind thing to do.

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

To use the windows key hook functions you need to DllImport a couple winapi functions:

要使用 windows key hook 函数,您需要 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); 

A fairly complete explanation and walkthrough can be found on CodeProject. Here is a direct linkto a self contained class file from that example that does everything (To get it to compile clean if you are using WPF will require you to manually reference System.Windows.Forms dll or just changing the 'System.Windows.Forms.Keys' reference to System.Windows.Input.Key should work).

可以在CodeProject上找到相当完整的解释和演练。这是来自该示例的自包含类文件的直接链接,该文件可以执行所有操作(如果您使用 WPF,要使其编译干净将需要您手动引用 System.Windows.Forms dll 或仅更改 'System.Windows.Forms.dll 文件)。 Forms.Keys 对 System.Windows.Input.Key 的引用应该可以工作)。

Remember to call UnhookWindowsHookEx() (the class does this in Dispose()) to unhook your captures or people will hate you.

请记住调用 UnhookWindowsHookEx()(该类在 Dispose() 中执行此操作)来取消您的捕获,否则人们会讨厌您。

回答by Tobiasopdenbrouw

You need a keyboard hook. Starts somewhere like this:

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

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

and continue like this:

并继续这样:

  LRESULT KeyboardProc(...)
  {
     if (Key == VK_SOMEKEY)
    return 1;             // Trap key


    return CallNextHookEx(...); // Let the OS handle it

  }

And for more detail: http://www.codeproject.com/KB/winsdk/AntonioWinLock.aspx

更多详情:http: //www.codeproject.com/KB/winsdk/AntonioWinLock.aspx

回答by ChrisBD

Assuming that you wish to disable the Windows key permenantly and not just when your code is in focus then you can do so by editing the registry as follows:

假设您希望永久禁用 Windows 键,而不仅仅是在您的代码处于焦点时,那么您可以通过如下编辑注册表来实现:

To disable: Add a new REG_BINARYvalue called "Scancode Map" to "HKEY_LOCAL_ MACHINE\System\CurrentControlSet\Control\Keyboard Layout" with a data value of "00000000000000000300000000005BE000005CE000000000"

禁用:添加一个新REG_BINARY叫做“值扫描码地图”到“ HKEY_LOCAL_ MACHINE \系统\ CurrentControlSet \控制\键盘布局”与“数据值00000000000000000300000000005BE000005CE000000000

To enable: Delete the "Scancode Map" value entirely from the registry.

启用:从注册表中完全删除“扫描码映射”值。