Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
>Hello,
I want to disable the Windows Key in c sharp but I can't. I was able to hide the taskbar and the start menu but not to disable the Windows Key.
Here is the code of what I did:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

using System.Windows.Forms;
using Microsoft.Win32;
namespace UserControl
{
    class Taskbar
    {
        [DllImport("user32.dll")]
    private static extern int FindWindow(string className, string windowText);

    [DllImport("user32.dll")]
    private static extern int ShowWindow(int hwnd, int command);

    [DllImport("user32.dll")]
    public static extern int FindWindowEx(int parentHandle, int childAfter, string className, int windowTitle);

    [DllImport("user32.dll")]
    private static extern int GetDesktopWindow();

    private const int SW_HIDE = 0;
    private const int SW_SHOW = 1;

    protected static int Handle
    {
        get
        {
            return FindWindow("Shell_TrayWnd", "");
        }
    }

    protected static int HandleOfStartButton
    {
        get
        {
            int handleOfDesktop = GetDesktopWindow();
            int handleOfStartButton = FindWindowEx(handleOfDesktop, 0, "button", 0);
            return handleOfStartButton;
        }
    }

    public Taskbar()
    {
        // hide ctor
    }

    public static void Show()
    {
        ShowWindow(Handle, SW_SHOW);
        ShowWindow(HandleOfStartButton, SW_SHOW);
        
    }

    public static void Hide()
    {
        ShowWindow(Handle, SW_HIDE);
        ShowWindow(HandleOfStartButton, SW_HIDE);
        
    }
        
    }
 
}


What I want is to add the Windows Key Disable in this class. And I must be able to Enable again if I close the appplication.
Thank you.

What I have tried:

I was able to hide the taskbar and the start menu but not to disable the Windows Key.
Posted
Updated 12-Jul-16 0:19am
Comments
[no name] 12-Jul-16 6:17am    
I'm not sure wether this is a good Praxis, but have a look here...
How to disable the Windows key | PC Gamer[^]
or also here...
Disable Windows Key | JohnHaller.com[^]
and here in c# (but also registry Manipulation)
how can I disable windows key in c#? - Stack Overflow[^]
Philippe Mori 12-Jul-16 9:12am    
You should not do that. Would you like if someone program would do that on your own computer? So don't do to other what you don't want others to do to you.
TatsuSheva 12-Jul-16 10:04am    
Haha don't worry it is a project work I am in a compagny and my employer asks me to do that.

1 solution

See this thread on StackOverflow for a variety of techniques: [^]

However ...

This kind of task is often found when using Windows in "kiosk" mode, or to drive a point-of-sale ui, or internet-cafe ui, and I suggest you research those if your goal is to really limit what the user can do with keyboard (and/or mouse).

But, consider that disabling the Windows key standard behavior (or the alt-tab key-combination default behavior, etc.) is probably going to make some users confused ... and/or angry.

Have you opened and read the links to "related questions" that appear on this page ?
 
Share this answer
 
v2
Comments
TatsuSheva 12-Jul-16 7:13am    
public static class WindowsKey {
///
/// Disables the Windows Key
///

/// <remarks>May require the current user to logoff or restart the system
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();
}
}

///
/// Enables the Windows Key
///

/// <remarks>May require the current user to logoff or restart the system
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();
}
}
}
This code doesn't work for me why ? Yes I read and I tried to use it in my app but I didn't find out.
BillWoodruff 12-Jul-16 7:54am    
There is no need to "dump" on-line code in a comment. There are other techniques shown on that thread on StackOverflow.
TatsuSheva 13-Jul-16 7:00am    
I found a solution for this problem but when I start the application in C# all the keys are blocked. I can't write anymore. Is there any solution ?
BillWoodruff 13-Jul-16 15:09pm    
I suggest you carefully compose and post another question here. Describe what you are doing now (we can't read your mind), and show the code you are using ... use the format code options in the CP Editor to format the code.

cheers, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900