How can I disable a Windows key in C #?

How to disable or lock the Windows button?

+6
c # windows button
source share
5 answers
/// <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(); } } } 
+3
source share

You need a hook for the keyboard. It starts like this:

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

and continue as follows:

  LRESULT KeyboardProc(...) { if (Key == VK_SOMEKEY) return 1; // Trap key return CallNextHookEx(...); // Let the OS handle it } 

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

+4
source share

Using window hooks is much cleaner than modifying the registry. In addition, sometimes people have their own personal scancode cards, and copying them is not very nice.

To use the Windows key lock functions, you need to execute DllImport several 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 reasonably complete explanation and walkthrough can be found on CodeProject . Here is a direct link to the standalone class file from this example, which does everything (To get it for compilation, if you use WPF, you need to either manually link to the System.Windows.Forms file or just change the link "System.Windows.Forms.Keys "on System.Windows.Input.Key to work).

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

+3
source share

Assuming you want to permanently disable the Windows key, and not just when your code is in focus, you can do this by editing the registry as follows:

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

To enable : Remove the " Scancode Map " completely from the registry.

+1
source share

fooobar.com/questions/870836 / ...

I believe that your question can be answered using this.
I would put this in the comments, but I don't have 50 points yet.

0
source share

All Articles