Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I disable windows key in c#?

Tags:

c#

windows

button

How can i disable or lock windows button?

like image 397
nectar Avatar asked Jul 29 '10 09:07

nectar


People also ask

How do I disable the Windows Key?

Click the plus button, then click the “Key” drop-down menu. Scroll down to “Win” and select that option. Now click the “Mapped To” drop-down menu and choose Disable. Click OK to confirm.

How do I lock the Windows button on my keyboard?

Please, press Fn + F6 to activate or deactivate Windows key. This procedure is compatible with computers and notebooks, regardless which brand are you using. Also, try pressing “Fn + Windows” key which can sometimes get it working again.

How do I disable Windows Key forever?

Press Windows Key + R and enter gpedit. Press Enter or click OK. Now navigate to the User Configuration > Administrative Templates > Windows Components > File Explorer in the left pane. In the right pane, locate and double click Turn off Windows Key hotkeys option.


1 Answers

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.

like image 84
Sogger Avatar answered Oct 04 '22 18:10

Sogger