Disable Keyboard Shortcuts and Combination Keys with C# (3): Disable Ctrl + Alt + Del

    In all combination keys on Windows platform Ctrl + Alt + Del is the most difficult to be disabled. The normal keyboard hook method is invalid. Developing a keyboard driver and replacing the Windows keyboard driver with it may be the perfect solution. But it is extremely hard. Fortunately someone recommended the following webpage on website stackoverflow.com:
http://www.northcode.com/blog.php/2007/07/25/Securing-Windows-For-Use-As-A-Kiosk

    An ingenious method was introduced there. It circumvented the difficulty of developing the keyboard driver. The basic idea is to reset keyboard scan code mapping by modifying Windows registry. The scan code mapping item is stored in the registry at:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout\Scancode Map
    It's difficult and danger to require common users to modify Windows registry. To develop a specialized program is a better choice. A C# program is given here. Note that the program MUST be run with Administrator Privileges. In order to make it take effect, to restart the computer is necessary.
    When it is run, the interface is as follows:

Disable Keyboard Shortcuts and Combination Keys with C# (3): Disable Ctrl + Alt + Del_第1张图片


    Ctrl key can be disabled or enabled by running the program. Other modifier keys, including WIN and ALT keys are not processed. It's easy to modify the program to disable those keys. When Ctrl key is disabled, all combination keys relating to it, such as Ctrl + Alt + Del and Ctrl + Shift + Esc are also disabled.

Step:
1. Create a C# Windows Forms application in Visual Studio. The project name is 'EnableOrDisableCtrlKey';
2. Switch from code view to designer view, drag a label into the Windows Form from the toolbox. The label text is 'Enable or Disable Ctrl Key?'.
3. Drag three buttons into the Windows Form from the toolbox, named them by EnableButton, DisableButton and ExitButton;
4. Modify the file 'Form1.cs', the source code is as listed below:

/**************************************************
* Author: HAN Wei
* Author's blog: http://blog.csdn.net/henter/
* Date: Dec. 6th, 2016
* Description: demonstrate how to disable Ctrl key. Combination keys relating to Ctrl, 
*              such as Ctrl + Alt + Del and Ctrl + Shift + Esc, will also be disabled.
**************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace EnableOrDisableCtrlKey
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void EnableButton_Click(object sender, EventArgs e)
        {
            RegistryKey rkLm = Registry.LocalMachine;
            RegistryKey rkLmSys = rkLm.OpenSubKey("SYSTEM");
            RegistryKey rkLmSysCcs = rkLmSys.OpenSubKey("CurrentControlSet");
            RegistryKey rkLmSysCcsCtrl = rkLmSysCcs.OpenSubKey("Control");
            RegistryKey rkLmSysCCsCtrlKl = rkLmSysCcsCtrl.OpenSubKey("Keyboard Layout", true);
            try
            {
                rkLmSysCCsCtrlKl.DeleteValue("Scancode Map", true);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message + "\n" + "Ctrl key has been enabled!\nYou don't need to enable it again.");
                return;
            }
            MessageBox.Show("Ctrl key is enabled!\nRestart computer to take effect!");
            rkLmSysCCsCtrlKl.Close();
        }

        private void DisableButton_Click(object sender, EventArgs e)
        {
            const string keyName = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Keyboard Layout";
            try
            {
                Registry.SetValue(keyName, "Scancode Map", new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0x3, 0, 0, 0, 
                    0, 0, 0x1d, 0, 0, 0, 0x1d, 0xe0, 0, 0, 0, 0 }, RegistryValueKind.Binary);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
                return;
            }
            MessageBox.Show("Ctrl key is disabled!\nRestart computer to take effect!");
        }

        private void ExitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

    }
}

5. Build the program, run it with Administrator Privileges. It is necessary to restart the computer if Ctrl key is enabled or disabled.

你可能感兴趣的:(C#)