Disable Keyboard Shortcuts and Combination Keys with C# (2): Disable Win + L

来源:互联网 发布:睡觉流口水 知乎 编辑:程序博客网 时间:2024/06/01 08:49
    The function of Win + L combination keys is to lock the computer or to switch users on Windows platform. The method introduced in the previous blog article can disable Win key, but cannot disable Win + L shortcut. So we must look for other ways. There is a DWORD (32-bit) value named by 'DisableLockWorkstation' in Windows registry. Its location is under the key:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System
    If there does not exist such a value, you can create it manually. To set the value as 0 will enable Win + L, to set the value as 1 will disable the combination. It's difficult and danger to require common users to modify Windows registry. To develop a specialized program is better. A C# program is given here. When it is run, the interface is as follows:


Step:
1. Create a C# Windows Forms application in Visual Studio. The project name is 'EnableOrDisableWinAndL';
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 Win + L ?'.
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. 5th, 2016* Description: demonstrate how to disable Win + L combination keys**************************************************/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 EnableOrDisableWinAndL{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        void SetRegistryValue(int value)        {            const string keyName = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";            try            {                Registry.SetValue(keyName, "DisableLockWorkstation", value, RegistryValueKind.DWord);            }            catch (Exception e)            {                MessageBox.Show(e.Message);            }        }        private void EnableButton_Click(object sender, EventArgs e)        {            SetRegistryValue(0);        }        private void DisableButton_Click(object sender, EventArgs e)        {            SetRegistryValue(1);        }        private void ExitButton_Click(object sender, EventArgs e)        {            Application.Exit();        }    }}


5. Build the program, run it with Administrator Privileges. It is unnecessary to restart the computer.



0 0
原创粉丝点击