在C#中使用全局鼠标、键盘Hook
今天,有个同事问我,怎样在C#中使用全局钩子?以前写的全局钩子都是用unmanaged C或C++写个DLL来实现,可大家都知道,C#是基于.Net Framework的,是managed,怎么实现全局钩子呢?于是开始到网上搜索,好不容易找到一篇,318804 - HOW TO: Set a Windows Hook in Visual C# .NET,里面详细的说明了如何使用鼠标钩子捕获鼠标的移动等,可是,它只能在Application里起作用,出了Application就没用了,就是说它还是没有实现全局钩子,而且文章结尾处说:“Global Hooks are not supported in the .NET Framework...”,这可怎么办呢?
别担心,办法总是有的,经过一番摸索以后,发现WH_KEYBORAD_LL和WH_MOUSE_LL这两个low-level的hook可以被安装成全局的,这就好办了,我们不妨用这两个low-level的hook替换掉WH_KEYBORAD和WH_MOUSE,于是开始测试。结果成功了,在C#里实现了全局钩子。
我们来看一下主要代码段。
首先倒入所需要的windows函数,主要有三个,SetWindowsHookEX用来安装钩子,UnhookWindowsHookEX用来卸载钩子以及CallNextHookEX用来将hook信息传递到链表中下一个hook处理过程。
- [DllImport("user32.dll", CharSet = CharSet.Auto,
- CallingConvention = CallingConvention.StdCall, SetLastError = true)]
- private static extern int SetWindowsHookEx(
- int idHook,
- HookProc lpfn,
- IntPtr hMod,
- int dwThreadId);
- [DllImport("user32.dll", CharSet = CharSet.Auto,
- CallingConvention = CallingConvention.StdCall, SetLastError = true)]
- private static extern int UnhookWindowsHookEx(int idHook);
- [DllImport("user32.dll", CharSet = CharSet.Auto,
- CallingConvention = CallingConvention.StdCall)]
- private static extern int CallNextHookEx(
- int idHook,
- int nCode,
- int wParam,
- IntPtr lParam);
- 下面是有关这两个low-level hook在Winuser.h中的定义:
- /// <summary>
- /// Windows NT/2000/XP: Installs a hook procedure that monitors low-level mouse input events.
- /// </summary>
- private const int WH_MOUSE_LL = 14;
- /// <summary>
- /// Windows NT/2000/XP: Installs a hook procedure that monitors low-level keyboard input events.
- /// </summary>
- private const int WH_KEYBOARD_LL = 13;
- 在安装全局钩子的时候,我们就要做替换了,将WH_MOUSE和WH_KEYBORAD分别换成WH_MOUSE_LL和WH_KEYBORAD_LL:
- //install hook
- hMouseHook = SetWindowsHookEx(
- WH_MOUSE_LL, //原来是WH_MOUSE
- MouseHookProcedure,
- Marshal.GetHINSTANCE(
- Assembly.GetExecutingAssembly().GetModules()[0]),
- 0);
- //install hook
- hKeyboardHook = SetWindowsHookEx(
- WH_KEYBOARD_LL, //原来是WH_KEYBORAD
- KeyboardHookProcedure,
- Marshal.GetHINSTANCE(
- Assembly.GetExecutingAssembly().GetModules()[0]),
- 0);
- 这样替换了之后,我们就可以实现全局钩子了,而且,不需要写DLL。看一下程序运行情况:
- 下面是关于鼠标和键盘的两个Callback函数:
- private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
- {
- // if ok and someone listens to our events
- if ((nCode >= 0) && (OnMouseActivity != null))
- {
- //Marshall the data from callback.
- MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));
- //detect button clicked
- MouseButtons button = MouseButtons.None;
- short mouseDelta = 0;
- switch (wParam)
- {
- case WM_LBUTTONDOWN:
- //case WM_LBUTTONUP:
- //case WM_LBUTTONDBLCLK:
- button = MouseButtons.Left;
- break;
- case WM_RBUTTONDOWN:
- //case WM_RBUTTONUP:
- //case WM_RBUTTONDBLCLK:
- button = MouseButtons.Right;
- break;
- case WM_MOUSEWHEEL:
- //If the message is WM_MOUSEWHEEL, the high-order word of mouseData member is the wheel delta.
- //One wheel click is defined as WHEEL_DELTA, which is 120.
- //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
- mouseDelta = (short)((mouseHookStruct.mouseData >> 16) & 0xffff);
- //TODO: X BUTTONS (I havent them so was unable to test)
- //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,
- //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released,
- //and the low-order word is reserved. This value can be one or more of the following values.
- //Otherwise, mouseData is not used.
- break;
- }
- //double clicks
- int clickCount = 0;
- if (button != MouseButtons.None)
- if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK) clickCount = 2;
- else clickCount = 1;
- //generate event
- MouseEventArgs e = new MouseEventArgs(
- button,
- clickCount,
- mouseHookStruct.pt.x,
- mouseHookStruct.pt.y,
- mouseDelta);
- //raise it
- OnMouseActivity(this, e);
- }
- //call next hook
- return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
- }
- private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
- {
- //indicates if any of underlaing events set e.Handled flag
- bool handled = false;
- //it was ok and someone listens to events
- if ((nCode >= 0) && (KeyDown != null || KeyUp != null || KeyPress != null))
- {
- //read structure KeyboardHookStruct at lParam
- KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
- //raise KeyDown
- if (KeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
- {
- Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
- KeyEventArgs e = new KeyEventArgs(keyData);
- KeyDown(this, e);
- handled = handled || e.Handled;
- }
- // raise KeyPress
- if (KeyPress != null && wParam == WM_KEYDOWN)
- {
- bool isDownShift = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false);
- bool isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false);
- byte[] keyState = new byte[256];
- GetKeyboardState(keyState);
- byte[] inBuffer = new byte[2];
- if (ToAscii(MyKeyboardHookStruct.vkCode,
- MyKeyboardHookStruct.scanCode,
- keyState,
- inBuffer,
- MyKeyboardHookStruct.flags) == 1)
- {
- char key = (char)inBuffer[0];
- if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key)) key = Char.ToUpper(key);
- KeyPressEventArgs e = new KeyPressEventArgs(key);
- KeyPress(this, e);
- handled = handled || e.Handled;
- }
- }
- // raise KeyUp
- if (KeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
- {
- Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
- KeyEventArgs e = new KeyEventArgs(keyData);
- KeyUp(this, e);
- handled = handled || e.Handled;
- }
- }
- //if event handled in application do not handoff to other listeners
- if (handled)
- return 1;
- else
- return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
- }
在C#中使用全局鼠标、键盘Hook的更多相关文章
- C#全局鼠标键盘Hook
原文出自:http://www.cnblogs.com/iEgrhn/archive/2008/02/17/1071392.html using System; using System.Collec ...
- 如何在C#中使用全局鼠标、键盘Hook
今天,有个同事问我,怎样在C#中使用全局钩子?以前写的全局钩子都是用unmanaged C或C++写个DLL来实现,可大家都知道,C#是基于.Net Framework的,是managed,怎么实现全 ...
- 全局鼠标钩子:WH_MOUSE_LL, 在【 win 10 上网本】上因为太卡,运行中丢失全局鼠标钩子
一台几年前买的上网本,让我安装了一个 win 10,然后用来测试程序的时候, 发现 使用 SetWindowsHookEx(WH_MOUSE_LL, mouseHook, GetModuleHandl ...
- 将CodedUI Test 放到控制台程序中,模拟鼠标键盘操作
CodedUI Test是微软的自动化测试工具,在VS中非常好用.可以用来模拟鼠标点击,键盘输入.但执行的时候必须要用mstest调用,无法传入参数(当然可以写入config文件中,但每次修改十分麻烦 ...
- [No0000AC]全局鼠标键盘模拟器
之前网上下载的一位前辈写的工具,名叫:Dragon键盘鼠标模拟器,网址http://www.esc0.com/. 本软件能够录制键盘鼠标操作,并能按要求回放,对于重复的键盘鼠标操作,可以代替人去做,操 ...
- Python——pyHook监听鼠标键盘事件
pyHook包为Windows中的全局鼠标和键盘事件提供回调. 底层C库报告的信息包括事件的时间,事件发生的窗口名称,事件的值,任何键盘修饰符等. 而正常工作需要pythoncom等操作系统的API的 ...
- hook 鼠标键盘消息实例分析
1.木马控制及通信方法包含:双管道,port重用.反弹技术.Hook技术,今天重点引用介绍一下hook的使用方法,hook信息后能够将结果发送到hacker邮箱等.实现攻击的目的. 转自:http:/ ...
- C#鼠标键盘钩子
using System;using System.Collections.Generic; using System.Reflection; using System.Runtime.Interop ...
- 键盘Hook【Delphi版】
原文:https://www.cnblogs.com/edisonfeng/archive/2012/05/18/2507858.html 一.钩子的基本概念 a) Hook作用:监视windows消 ...
随机推荐
- ajax连接池和XMLHttpRequest
连接池 我们公司在路由和交换机web界面和后端交互全部采用的是自己封装的ajax组件完成的,组件有点老了,代码风格和其中的某些用法现在看起来都有点不习惯.今天把这个组件的核心部分的ajax连接池记录下 ...
- VM虚拟机快照还原效果实现方式
===================================================默认"快照删除"行为:============================ ...
- boost的link 和 runtime-link,搭配shared 和 static
转自:http://blog.csdn.net/yasi_xi/article/details/8660549 link:生成动态链接库/静态链接库.生成动态链接库需使用shared方式,生成静态链接 ...
- DzzOffice结合office web Apps私有部署的实例
很多朋友都想在自己使用的DzzOffice中,调用本地部署的office web Apps server. 下面我就直接从头到尾的将部署全部过程分享给大家. 准备条件:两台服务器,配置稍高点,以免卡顿 ...
- 二级指针的作用及用途 .xml
pre{ line-height:1; color:#9f1d66; background-color:#e1e1e1; font-size:16px;}.sysFunc{color:#5d57ff; ...
- Tcl之group arguments
1 doubel quotes This allows substitutions to occur within the quotations - or "interpolation&qu ...
- DOS 命令大全
MS DOS 命令大全 一.基础命令 1 dir 无参数:查看当前所在目录的文件和文件夹. /s:查看当前目录已经其所有子目录的文件和文件夹. /a:查看包括隐含文件的所有文件. /ah:只显示出隐含 ...
- Yarn中的几种状态机
1 概述 为了增大并发性,Yarn采用事件驱动的并发模型,将各种处理逻辑抽象成事件和调度器,将事件的处理过程用状态机表示.什么是状态机? 如果一个对象,其构成为若干个状态,以及触发这些状态发生相互转移 ...
- 第二百二十天 how can I 坚持
今天如愿去了凤凰岭,比想象中的好多了.山好陡,都没有爬到山顶,下山时山都有点黑了,有点恐怖. 凤凰岭啊.有时间还得再去趟. 下周去八大处.坚持. 看什么时候能把北京这些山爬完,然后 三山五岳. 不爽. ...
- Spark RDD概念学习系列之RDD的转换(十)
RDD的转换 Spark会根据用户提交的计算逻辑中的RDD的转换和动作来生成RDD之间的依赖关系,同时这个计算链也就生成了逻辑上的DAG.接下来以“Word Count”为例,详细描述这个DAG生成的 ...