1

我正在使用下面的代码来禁用像 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
            }
        }
    }
}
4

2 回答 2

5

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

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

于 2009-07-27T09:36:00.320 回答
3

为什么不考虑通过组策略或编辑注册表来做到这一点呢?

于 2009-07-27T09:26:42.570 回答