how can I disable windows key in c#?

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.

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

[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 link to 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).

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


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


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

Tags:

Windows

C#

Button