迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
钩子(Hook),是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息,而且所监视的窗口可以是其他进程所创建的。当消息到达后,在目标窗口处理函数之前处理它。钩子机制允许应用程序截获处理window消息或特定事件。 LRESULT CALLBACKHookProc ( int nCode, WPARAM wParam, LPARAM lParam );
HHOOKSetWindowsHookEx( int idHook, // 钩子的类型,即它处理的消息类型 HOOKPROC lpfn, // 钩子子程的地址指针。如果dwThreadId参数为0 // 或是一个由别的进程创建的线程的标识, // lpfn必须指向DLL中的钩子子程。 // 除此以外,lpfn可以指向当前进程的一段钩子子程代码。 // 钩子函数的入口地址,当钩子钩到任何消息后便调用这个函数。 HINSTANCE hMod, // 应用程序实例的句柄。标识包含lpfn所指的子程的 DLL。 // 如果dwThreadId 标识当前进程创建的一个线程, // 而且子程代码位于当前进程,hMod必须为NULL。 // 可以很简单的设定其为本应用程序的实例句柄。 DWORD dwThreadId // 与安装的钩子子程相关联的线程的标识符。 // 如果为0,钩子子程与所有的线程关联,即为全局钩子。 );
LRESULTCallNextHookEx ( HHOOK hhk; int nCode; WPARAM wParam; LPARAM lParam; );
UnHookWindowsHookEx ( HHOOK hhk; );
#pragmadata_seg("SharedDataName") HHOOK hHook=NULL; #pragma data_seg()
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Reflection; using System.Diagnostics; using Microsoft.Win32; namespace Vjsdn.Tech.KeyboardHook { public partial class frmKeyboardHook : Form { //勾子管理类 private KeyboardHookLib _keyboardHook = null; public frmKeyboardHook() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //安装勾子 _keyboardHook = new KeyboardHookLib(); _keyboardHook.InstallHook(this.OnKeyPress); } private void button2_Click(object sender, EventArgs e) { //取消勾子 if (_keyboardHook != null) _keyboardHook.UninstallHook(); } /// <summary> /// 客户端键盘捕捉事件. /// </summary> /// <param name="hookStruct">由Hook程序发送的按键信息</param> /// <param name="handle">是否拦截</param> public void OnKeyPress(KeyboardHookLib.HookStruct hookStruct, out bool handle) { handle = false; //预设不拦截任何键 if (hookStruct.vkCode == 91) // 截获左win(开始菜单键) { handle = true; } if (hookStruct.vkCode == 92)// 截获右win { handle = true; } //截获Ctrl+Esc if (hookStruct.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control) { handle = true; } //截获alt+f4 if (hookStruct.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt) { handle = true; } //截获alt+tab if (hookStruct.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt) { handle = true; } //截获F1 if (hookStruct.vkCode == (int)Keys.F1) { handle = true; } //截获Ctrl+Alt+Delete if ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt + (int)Keys.Delete) { handle = true; } //如果键A~Z if (hookStruct.vkCode >= (int)Keys.A && hookStruct.vkCode <= (int)Keys.Z) { //挡掉B键 if (hookStruct.vkCode == (int)Keys.B) hookStruct.vkCode = (int)Keys.None; //设键为0 handle = true; } Keys key = (Keys)hookStruct.vkCode; label1.Text = "你按下:" + (key == Keys.None ? "" : key.ToString()); } } }
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Reflection; using System.Diagnostics; using Microsoft.Win32; using System.Windows.Forms; namespace Vjsdn.Tech.KeyboardHook { /// <summary> /// 键盘Hook管理类, by www.vjsdn.com 易学原创 /// </summary> public class KeyboardHookLib { private const int WH_KEYBOARD_LL = 13; //键盘 //键盘处理事件委托 ,当捕获键盘输入时调用定义该委托的方法. private delegate int HookHandle(int nCode, int wParam, IntPtr lParam); //客户端键盘处理事件 public delegate void ProcessKeyHandle(HookStruct param, out bool handle); //接收SetWindowsHookEx返回值 private static int _hHookValue = 0; //勾子程序处理事件 private HookHandle _KeyBoardHookProcedure; //Hook结构 [StructLayout(LayoutKind.Sequential)] public class HookStruct { public int vkCode; public int scanCode; public int flags; public int time; public int dwExtraInfo; } //设置钩子 [DllImport("user32.dll")] private static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId); //取消钩子 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] private static extern bool UnhookWindowsHookEx(int idHook); //调用下一个钩子 [DllImport("user32.dll")] private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam); //获取当前线程ID [DllImport("kernel32.dll")] private static extern int GetCurrentThreadId(); //Gets the main module for the associated process. [DllImport("kernel32.dll")] private static extern IntPtr GetModuleHandle(string name); private IntPtr _hookWindowPtr = IntPtr.Zero; //构造器 public KeyboardHookLib() { } //外部调用的键盘处理事件 private static ProcessKeyHandle _clientMethod = null; /// <summary> /// 安装勾子 /// </summary> /// <param name="hookProcess">外部调用的键盘处理事件</param> public void InstallHook(ProcessKeyHandle clientMethod) { _clientMethod = clientMethod; // 安装键盘钩子 if (_hHookValue == 0) { _KeyBoardHookProcedure = new HookHandle(OnHookProc); _hookWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); _hHookValue = SetWindowsHookEx( WH_KEYBOARD_LL, _KeyBoardHookProcedure, _hookWindowPtr, 0); //如果设置钩子失败. if (_hHookValue == 0) UninstallHook(); } } //取消钩子事件 public void UninstallHook() { if (_hHookValue != 0) { bool ret = UnhookWindowsHookEx(_hHookValue); if (ret) _hHookValue = 0; } } //钩子事件内部调用,调用_clientMethod方法转发到客户端应用。 private static int OnHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { //转换结构 HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); if (_clientMethod != null) { bool handle = false; //调用客户提供的事件处理程序。 _clientMethod(hookStruct, out handle); if (handle) return 1; //1:表示拦截键盘,return 退出 } } return CallNextHookEx(_hHookValue, nCode, wParam, lParam); } } } |
请发表评论