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:



    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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值