Пример #1
0
        static int hKeyboardHook = 0; //声明键盘钩子处理的初始值
        #endregion

        #region 公共方法
        /// <summary>
        /// 安装键盘钩子
        /// </summary>
        public void Start()
        {
            if (hKeyboardHook == 0)
            {
                KeyboardHookProcedure = new BaseWin32Api.HookProcess(KeyboardHookProcess);
                ProcessModule cModule  = Process.GetCurrentProcess().MainModule;
                var           mhIntPrt = BaseWin32Api.GetModuleHandle(cModule.ModuleName);

                hKeyboardHook = BaseWin32Api.SetWindowsHookEx(ConstDefintion.WH_KEYBOARD_LL, KeyboardHookProcedure, mhIntPrt, 0);
                data          = new KeyboardHookModel();
                if (hKeyboardHook == 0)
                {
                    Stop();
                    throw new Exception("安装键盘钩子失败");
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 启动全局钩子
        /// </summary>
        public void Start()
        {
            // 安装鼠标钩子
            if (_hMouseHook == 0)
            {
                // 生成一个HookProc的实例.
                _mouseHookProcedure = new BaseWin32Api.HookProcess(MouseHookProc);

                ProcessModule cModule  = Process.GetCurrentProcess().MainModule;
                var           mhIntPrt = BaseWin32Api.GetModuleHandle(cModule.ModuleName);

                _hMouseHook = BaseWin32Api.SetWindowsHookEx(ConstDefintion.WH_MOUSE_LL, _mouseHookProcedure, mhIntPrt, 0);

                //假设装置失败停止钩子
                if (_hMouseHook == 0)
                {
                    Stop();
                    throw new Exception("安装鼠标钩子失败...");
                }
            }
        }