c# keyboard hook works for only 10 times then it stops (or is supressed)?

不打扰是莪最后的温柔 提交于 2020-01-03 03:48:06

问题


It is the classic low level keyboard hook that I have used several times before quite successfully but lately it does not work right. It reads the keyboard inputs for exactly 10 times (10 times from any of the keys defined in HookCallback function) and then it stops. It is like some security feature of the OS or some other program like maybe an anivirus is getting in the way and suppressing the keyboard hook after 10 times.

Has anyone else encountered this 10 times limit? What could be the problem, in your opinion? I am using Windows 7 64 Bit and avast free antivirus. The program is written in c# 2008.

Here is the relevant code from program.cs:

 static class Program
{

    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private static LowLevelKeyboardProc _proc = HookCallback;
    private static IntPtr _hookID = IntPtr.Zero;


    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);

    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);




    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        _hookID = SetHook(_proc);
        Application.Run(new Form1());
        UnhookWindowsHookEx(_hookID);
    }





    private static IntPtr SetHook(LowLevelKeyboardProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
        }
    }

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {
            int vkCode = Marshal.ReadInt32(lParam);

            if ((Keys)vkCode == Keys.Delete)
            {
                Form1.staticmethod1();
            }
            else if ((Keys)vkCode == Keys.F8)
            {
                Form1.staticmethod2();
            }
            else if ((Keys)vkCode == Keys.F9)
            {
                Form1.staticmethod3();
            }

        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

Thanks in advance.

来源:https://stackoverflow.com/questions/22407292/c-sharp-keyboard-hook-works-for-only-10-times-then-it-stops-or-is-supressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!