Exemple #1
0
        /// <summary>
        /// Enables the hook
        /// </summary>
        /// <returns>True if hook is enabled</returns>
        public bool EnableHook()
        {
            if (this.mseHook != IntPtr.Zero)
            {
                return(true);
            }

            this.mseHook = USER32.SetWindowsHookEx(HookType.WH_MOUSE_LL, this.mseCallbackDelegate, KERNEL32.GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName), 0);
            return(this.mseHook != IntPtr.Zero);
        }
Exemple #2
0
        public static void HookKeyboard(HookProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())
                using (ProcessModule curModule = curProcess.MainModule)
                {
                    var module = KERNEL32.GetModuleHandle(curModule.ModuleName);

                    UnhookKeyboard();

                    HookProc hook = (int nCode, IntPtr wParam, IntPtr lParam) => {
                        proc(nCode, wParam, lParam);

                        return(USER32.CallNextHookEx(_hHook, nCode, wParam, lParam));
                    };

                    _hHook = USER32.SetWindowsHookEx(WH_KEYBOARD_LL, hook, module, 0);
                }
        }