c#使用Hook钩子全局监听键盘和鼠标 钩子全局监听键盘和鼠标 Hook 使用 c#
背景:今天接到客户一个需求,就是在收银员在用扫码枪扫顾客会员码或者微信付款码的时候判断用户有没有加企微好友和进企微群,然后根据这个状态进行语音播报,判断顾客能不能享受优惠价。关键难点就是用户用的收银系统是别家的,线上小程序用的是我们家的,两家不互通,所以立即决定采用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 StringBuilder codes = new StringBuilder(); //接收扫码枪输入的数据
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
if (nCode >= 0)
{
if(wParam == 0x100)
{
KeyMSG m = (KeyMSG)Marshal.PtrToStructure(lParam, typeof(KeyMSG));
if (m.vkCode == 0x31)
{
codes.Append(1);
}
else if (m.vkCode == 0x32)
{
codes.Append(2);
}
else if (m.vkCode == 0x33)
{
codes.Append(3);
}
else if (m.vkCode == 0x34)
{
codes.Append(4);
}
else if (m.vkCode == 0x35)
{
codes.Append(5);
}
else if (m.vkCode == 0x36)
{
codes.Append(6);
}
else if (m.vkCode == 0x37)
{
codes.Append(7);
}
else if (m.vkCode == 0x38)
{
codes.Append(8);
}
else if (m.vkCode == 0x39)
{
codes.Append(9);
}
else if (m.vkCode == 0x30)
{
codes.Append(0);
}
else if (m.vkCode == 0x60)
{
codes.Append(0);
}
else if (m.vkCode == 0x61)
{
codes.Append(1);
}
else if (m.vkCode == 0x62)
{
codes.Append(2);
}
else if (m.vkCode == 0x63)
{
codes.Append(3);
}
else if (m.vkCode == 0x64)
{
codes.Append(4);
}
else if (m.vkCode == 0x65)
{
codes.Append(5);
}
else if (m.vkCode == 0x66)
{
codes.Append(6);
}
else if (m.vkCode == 0x67)
{
codes.Append(7);
}
else if (m.vkCode == 0x68)
{
codes.Append(8);
}
else if (m.vkCode == 0x69)
{
codes.Append(9);
}
if (m.vkCode == 0x0D)
{
// 回车键
string code = codes.ToString();
Trace.WriteLine(code);
if(code.Length == 18)
{
StatausHandle(code);
codes.Clear();
}
}
}
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
- 卸载键盘钩子和鼠标钩子
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就算扫码枪输入的条码,根据这个条码实现自己的判断逻辑来进行语音播报就行了。