7

How can i disable or lock windows button?

  • permenantly or just while your programme is running? – ChrisBD Jul 29 '10 at 9:45
  • 3
    Just the windows button, or stuff like Ctrl+Esc too? – Greg Hewgill Jul 29 '10 at 9:45
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();
            }
        }
    }
| improve this answer | |
  • Request Registry Access is not allowed error show any solution. – Ahmad May 9 '18 at 7:40
6

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.

| improve this answer | |
4

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

| improve this answer | |
1

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:

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

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

| improve this answer | |
  • +1, thanks for that. I'd not be happy with the guy who wrecked my keyboard though. – Tobiasopdenbrouw Jul 29 '10 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.