Beispiel #1
0
        /// <summary>
        /// 停止钩子
        /// </summary>
        public void Stop()
        {
            bool retKeyboard = true;

            if (hKeyboardHook != 0)
            {
                retKeyboard   = BaseWin32Api.UnhookWindowsHookEx(hKeyboardHook);
                hKeyboardHook = 0;
            }

            if (!(retKeyboard))
            {
                throw new Exception("卸载钩子失败!");
            }
        }
Beispiel #2
0
        /// <summary>
        /// 停止全局钩子
        /// </summary>
        public void Stop()
        {
            bool retMouse = true;

            if (_hMouseHook != 0)
            {
                retMouse    = BaseWin32Api.UnhookWindowsHookEx(_hMouseHook);
                _hMouseHook = 0;
            }

            // 假设卸下钩子失败
            if (!(retMouse))
            {
                throw new Exception("UnhookWindowsHookEx failed.");
            }
        }
Beispiel #3
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("安装键盘钩子失败");
                }
            }
        }
Beispiel #4
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("安装鼠标钩子失败...");
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// 鼠标钩子回调函数
        /// </summary>
        private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            // 假设正常执行而且用户要监听鼠标的消息
            if ((nCode >= 0))
            {
                var eventArgs = GetMouseEventArgs(wParam, lParam);

                switch (wParam)
                {
                case ConstDefintion.WM_LBUTTONDOWN:
                    OnMouseClickHanler(this, eventArgs);
                    break;

                case ConstDefintion.WM_LBUTTONUP:
                    break;

                case ConstDefintion.WM_LBUTTONDBLCLK:
                    break;

                case ConstDefintion.WM_MOUSEMOVE:
                    OnMouseMoveHandler(this, eventArgs);
                    break;

                case ConstDefintion.WM_RBUTTONDOWN:
                    break;

                case ConstDefintion.WM_RBUTTONUP:
                    break;

                case ConstDefintion.WM_RBUTTONDBLCLK:
                    break;
                }
            }

            //启动下一次钩子
            return(BaseWin32Api.CallNextHookEx(_hMouseHook, nCode, wParam, lParam));
        }
Beispiel #6
0
        /// <summary>
        /// 获取键盘消息
        /// </summary>
        /// <param name="nCode"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private int KeyboardHookProcess(int nCode, Int32 wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                var message = (BaseWin32Api.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(BaseWin32Api.KeyboardHookStruct));
                if (wParam == ConstDefintion.WM_KEYUP)
                {
                    #region 数据处理
                    data.VirtKey  = message.vkCode & 0xff;   //虚拟吗
                    data.ScanCode = message.scanCode & 0xff; //扫描码
                    StringBuilder keyName = new StringBuilder(225);
                    if (BaseWin32Api.GetKeyNameText(data.ScanCode * 65536, keyName, 255) > 0)
                    {
                        data.KeyName = keyName.ToString().Trim(new char[] { ' ', '\0' });
                    }
                    else
                    {
                        data.KeyName = "";
                    }
                    byte[] kbArray = new byte[256];
                    uint   uKey    = 0;
                    BaseWin32Api.GetKeyboardState(kbArray);

                    if (BaseWin32Api.ToAscii(data.VirtKey, data.ScanCode, kbArray, ref uKey, 0))
                    {
                        data.Ascll = uKey;
                        data.Chat  = Convert.ToChar(uKey);
                    }

                    TimeSpan ts = DateTime.Now.Subtract(data.Time);
                    #endregion

                    #region 手动输入
                    //时间戳,大于50 毫秒表示手动输入
                    if (ts.TotalMilliseconds > 50)
                    {
                        keyCodeStr   = data.Chat.ToString();
                        data.Code    = keyCodeStr;
                        data.IsValid = false;
                        TextHelper.Write($"json -01 ->{JsonConvert.SerializeObject(data)}");
                    }
                    #endregion

                    #region 扫码枪或者刷脸设备键盘
                    else
                    {
                        if (keyCodeStr.Length > 5)
                        {
                            data.IsValid = true;
                        }
                        keyCodeStr += data.Chat.ToString();
                        data.Code   = keyCodeStr;
                        TextHelper.Write($"json -02 ->{JsonConvert.SerializeObject(data)}");
                    }
                    data.Time = DateTime.Now;
                    #endregion

                    #region 数据传输事件
                    if (OnDataEvent != null && wParam == ConstDefintion.WM_KEYUP)
                    {
                        OnDataEvent(data);
                    }
                    #endregion
                }
            }
            return(BaseWin32Api.CallNextHookEx(hKeyboardHook, nCode, wParam, lParam));
        }