C# 注册Win+时出现问题;c中的L热键#

C# 注册Win+时出现问题;c中的L热键#,c#,windows,hotkeys,C#,Windows,Hotkeys,我正在使用下面的代码来禁用热键,如Alt+f4、ctrl+c,这些热键工作正常。但我无法使用下面的代码注册win+L namespace KioskMode { public partial class Test : Form { #region Dynamic Link Library Imports [DllImport("user32.dll")] private static extern int FindWindow(s

我正在使用下面的代码来禁用热键,如Alt+f4、ctrl+c,这些热键工作正常。但我无法使用下面的代码注册win+L

namespace KioskMode
{
    public partial class Test : Form
    {
        #region Dynamic Link Library Imports

        [DllImport("user32.dll")]
        private static extern int FindWindow(string cls, string wndwText);

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

        [DllImport("user32.dll")]
        private static extern long SHAppBarMessage(long dword, int cmd);

        [DllImport("user32.dll")]
        private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);

        [DllImport("user32.dll")]
        private static extern int UnregisterHotKey(IntPtr hwnd, int id);

        #endregion


        #region Modifier Constants and Variables

        // Constants for modifier keys
        private const int USE_ALT = 1;
        private const int USE_CTRL = 2;
        private const int USE_SHIFT = 4;
        private const int USE_WIN = 8;

        // Hot key ID tracker
        short mHotKeyId = 0;

        #endregion

        public Test()
        {
            InitializeComponent();
            RegisterGlobalHotKey(Keys.F4, USE_ALT);
            RegisterGlobalHotKey(Keys.L, USE_WIN);
        }

        private void RegisterGlobalHotKey(Keys hotkey, int modifiers)
        {
            try
            { 
                mHotKeyId++;

                if (mHotKeyId > 0)
                {
                    // register the hot key combination
                    if (RegisterHotKey(this.Handle, mHotKeyId, modifiers, Convert.ToInt16(hotkey)) == 0)
                    {
                          MessageBox.Show("Error: " + mHotKeyId.ToString() + " - " +
                            Marshal.GetLastWin32Error().ToString(),
                            "Hot Key Registration");
                    }
                }
            }
            catch
            { 
                UnregisterGlobalHotKey();
            }
        }


        private void UnregisterGlobalHotKey()
        { 
            for (int i = 0; i < mHotKeyId; i++)
            {
                UnregisterHotKey(this.Handle, i);
            }
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m); 
            const int WM_HOTKEY = 0x312;
            if (m.Msg == WM_HOTKEY)
            {
                // Ignore the request or each
                // disabled hotkey combination
            }
        }
    }
}
namespace KioskMode
{
公共部分类测试:表单
{
#区域动态链接库导入
[DllImport(“user32.dll”)]
私有静态外部程序int FindWindow(字符串cls,字符串wndwText);
[DllImport(“user32.dll”)]
私有静态外部显示窗口(inthwnd,intcmd);
[DllImport(“user32.dll”)]
私有静态外部long-SHAppBarMessage(long-dword,int-cmd);
[DllImport(“user32.dll”)]
私有静态外部内部寄存器hotkey(IntPtr hwnd、intid、intfsmodifiers、intvk);
[DllImport(“user32.dll”)]
私有静态外部int unregister热键(IntPtr hwnd,int id);
#端区
#区域修改器常量和变量
//修改器键的常量
私有常量int USE_ALT=1;
私有常量int USE_CTRL=2;
私有常量int USE_SHIFT=4;
私用const int USE_WIN=8;
//热键ID跟踪器
短mHotKeyId=0;
#端区
公开考试()
{
初始化组件();
RegisterGlobalHotKey(key.F4,使用_ALT);
RegisterGlobalHotKey(key.L,使用_-WIN);
}
私有void注册表全局热键(键热键、int修饰符)
{
尝试
{ 
mHotKeyId++;
如果(mHotKeyId>0)
{
//注册热键组合
if(RegisterHotKey(this.Handle,mHotKeyId,modifiers,Convert.ToInt16(热键))=0)
{
MessageBox.Show(“错误:+mHotKeyId.ToString()+”-“+
Marshal.GetLastWin32Error().ToString(),
“热键注册”);
}
}
}
抓住
{ 
取消注册全局热键();
}
}
私有void UnregisterGlobalHotKey()
{ 
for(int i=0;i
为什么不研究通过组策略执行此操作,或者改为编辑注册表?

为什么不研究通过组策略执行此操作,或者改为编辑注册表?

您无法注册它,因为Windows已经将其用作热键。如果你真的想这么做,你必须注册一个低级键盘钩子


您可以注册Alt+F4、Ctrl+C的原因。。。这些密钥不是热键(它们只是在wndproc中处理的)。

您无法注册它,因为Windows已经将其用作热键。如果你真的想这么做,你必须注册一个低级键盘钩子

您可以注册Alt+F4、Ctrl+C的原因。。。这些键不是热键(它们只是在wndproc中处理)。

Windows键是“为操作系统保留的”,因此任何对它的使用都将是不可靠的,并且会对用户产生不利影响:试图使用Win-L,您将应用程序的需求置于用户需求之上。@Richard-我认为(基于同一类的名称)karthik正在创建一个Kiosk应用程序,而不是一个普通的桌面应用程序。Windows键是“为操作系统保留的”,因此任何对它的使用都将是不可靠的,并且会对用户产生不利影响:试图使用Win-L,你将应用程序的需求置于用户需求之上。@Richard-我认为(基于同一类的名称)karthik正在创建一个Kiosk应用程序,而不是一个普通的桌面应用程序。