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

    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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值