|
背景:今天接到客户一个需求,就是在收银员在用扫码枪扫顾客会员码或者微信付款码的时候判断用户有没有加企微好友和进企微群,然后根据这个状态进行语音播报,判断顾客能不能享受优惠价。关键难点就是用户用的收银系统是别家的,线上小程序用的是我们家的,两家不互通,所以立即决定采用Hook钩子技术做一工具挂在其他收银系统上。
Hook钩子
Windows是一个不断处理消息的系统,每次的鼠标点击移动键盘输入都是消息。钩子是系统消息处理的一环,可以使用钩子处理函数来监听消息传送,并处理消息。
思路
- 用WPF做一个无边框小小的半透明圆形窗体使用TopMost长驻留在桌面上。
- 增加一个ContextMenu 菜单,安装钩子和卸载钩子
- 接入钩子API
- #region 第一步:声明API函数。
- // 安装钩子
- [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
- public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
- // 卸载钩子
- [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
- public static extern bool UnhookWindowsHookEx(int idHook);
- // 继续下一个钩子
- [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
- public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
- // 取得当前线程编号
- [DllImport("kernel32.dll")]
- static extern int GetCurrentThreadId();
- #endregion
复制代码- public struct KeyMSG
- {
- public int vkCode;
- public int scanCode;
- public int flags;
- public int time;
- public int dwExtraInfo;
- }
- public struct MSG
- {
- public Point p;
- public IntPtr HWnd;
- public uint wHitTestCode;
- public int dwExtraInfo;
- }
复制代码- #region 第二步:声明,定义委托
- public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
- static int hKeyboardHook = 0;//如果hKeyboardHook不为0则说明钩子安装成功
- HookProc KeyboardHookProcedure;
- static int hMouseHook = 0;
- HookProc mouseHookProcedure;
- #endregion
- private void install_Click(object sender, RoutedEventArgs e)
- {
- HookStart();
- }
- private void HookStart()
- {
- if (hKeyboardHook == 0)//如果hKeyboardHook==0,钩子安装失败
- {
- //创建HookProc实例
- KeyboardHookProcedure = new HookProc(KeyboardHookProc);
- //设置全局钩子
- hKeyboardHook = SetWindowsHookEx(13, KeyboardHookProcedure,
- Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
- if (hKeyboardHook == 0)
- {
- //终止钩子
- throw new Exception("安装钩子失败");
- }
- Trace.WriteLine("键盘钩子安装成功");
- }
- if(hMouseHook == 0)
- {
- mouseHookProcedure = new HookProc(MouseHookProc);
- hMouseHook = SetWindowsHookEx(14, mouseHookProcedure,
- Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
- if(hMouseHook == 0)
- {
- throw new Exception("安装鼠标钩子失败");
- }
- Trace.WriteLine("鼠标钩子安装成功");
- }
- }
复制代码- private void unInstall_Click(object sender, RoutedEventArgs e)
- {
- HookStop();
- }
- private void HookStop()
- {
- bool retKeyboard = true;
- if (hKeyboardHook != 0)
- {
- retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
- hKeyboardHook = 0;
- }
- if (!retKeyboard)
- throw new Exception("钩子卸载失败");
- Trace.WriteLine("键盘钩子卸载成功");
- if (hMouseHook != 0)
- {
- retKeyboard = UnhookWindowsHookEx(hMouseHook);
- hMouseHook = 0;
- }
- if (!retKeyboard)
- throw new Exception("鼠标钩子卸载失败");
- Trace.WriteLine("鼠标钩子卸载成功");
- }
复制代码
- 好了,拿到的code就算扫码枪输入的条码,根据这个条码实现自己的判断逻辑来进行语音播报就行了。
来源:https://www.cnblogs.com/leafroc/p/17980022
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
|