使用c#阻止快捷键

时间:2009-07-24 03:54:29

标签: c# keyboard-hook

我使用以下代码禁用Alt + Tab,Alt + Esc,Ctrl + Esc和Windows Key,但不知何故它无效。请帮我纠正。

namespace BlockShortcuts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private delegate int LowLevelKeyboardProcDelegate(int nCode, int
           wParam, ref KBDLLHOOKSTRUCT lParam);

        [DllImport("user32.dll", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi)]
        private static extern int SetWindowsHookEx(
           int idHook,
           LowLevelKeyboardProcDelegate lpfn,
           int hMod,
           int dwThreadId);

        [DllImport("user32.dll")]
        private static extern int UnhookWindowsHookEx(int hHook);

        [DllImport("user32.dll", EntryPoint = "CallNextHookEx", CharSet = CharSet.Ansi)]
        private static extern int CallNextHookEx(
            int hHook, int nCode,
            int wParam, ref KBDLLHOOKSTRUCT lParam);

        const int WH_KEYBOARD_LL = 13;
        private int intLLKey;
        private KBDLLHOOKSTRUCT lParam;

        private struct KBDLLHOOKSTRUCT
        {
            public int vkCode;
            int scanCode;
            public int flags;
            int time;
            int dwExtraInfo;
        }

        private int LowLevelKeyboardProc(
            int nCode, int wParam,
            ref KBDLLHOOKSTRUCT lParam)
        {
            bool blnEat = false;
            switch (wParam)
            {
                case 256:
                case 257:
                case 260:
                case 261:
                    //Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key
                    if (((lParam.vkCode == 9) && (lParam.flags == 32)) ||
                    ((lParam.vkCode == 27) && (lParam.flags == 32)) || ((lParam.vkCode ==
                    27) && (lParam.flags == 0)) || ((lParam.vkCode == 91) && (lParam.flags
                    == 1)) || ((lParam.vkCode == 92) && (lParam.flags == 1)) || ((true) &&
                    (lParam.flags == 32)))
                    {
                        blnEat = true;
                    }
                    break;
            }

            if (blnEat)
                return 1;
            else return CallNextHookEx(0, nCode, wParam, ref lParam);

        }

        private void KeyboardHook(object sender, EventArgs e)
        {
            intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL,new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc),
                       System.Runtime.InteropServices.Marshal.GetHINSTANCE(
                       System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32(), 0);
        }

        private void ReleaseKeyboardHook()
        {
            intLLKey = UnhookWindowsHookEx(intLLKey);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                KeyboardHook(this, e);
            else
                ReleaseKeyboardHook();
        }
    }
}

2 个答案:

答案 0 :(得分:6)

一般来说代码工作得很好。您目击的效果可能源于在Visual Studio调试器下运行,这通常意味着您正在运行 Visual Studio Hosting Process (vshost.exe)

这意味着System.Reflection.Assembly.GetExecutingAssembly()函数中对KeyboardHook()的调用返回的是vshost.exe而不是可执行文件,因此无法实现为可执行文件安装挂钩所需的效果。

因此,要查看您的代码生效,您必须执行以下操作之一:

  • 在Visual Studio外部运行
  • 在Visual Studio中运行它,但是通过菜单'Debug' - >'Start without Debugging'
  • 禁用Visual Studio Hosting Process,见下文

请注意,你可以disable the Visual Studio Hosting Process,但请注意潜在的副作用,引用:

  

禁用托管过程时   几个调试功能   不可用或经验减少   性能。有关更多信息,请参阅   Debugging and the Hosting Process

答案 1 :(得分:1)

您可以查看我对here相关问题的回答。 请注意RegisterLowLevelHook方法的区别(您称之为KeyboardHook方法,因此您知道要比较的内容)。即使从VS调试,我也没有遇到任何问题。 基本上,正如其他人所说,不要使用GetExecutingAssembly方法,而是使用我在其他答案中列出的方法。

以下是您感兴趣的一项功能的摘录:

private IntPtr RegisterLowLevelHook(LowLevelKeyboardProc hook)
{
    IntPtr handle = IntPtr.Zero;

    using (Process currentProcess = Process.GetCurrentProcess())
    using (ProcessModule currentModule = currentProcess.MainModule)
    {
        IntPtr module = Kernel32.GetModuleHandle(currentModule.ModuleName);
        handle = User32.SetWindowsHookEx(HookType.KEYBOARD_LL, hook, module, 0);
    }

    return handle;
}