1、键盘钩子实例一、界面样式如下: 主要功能如下:安装钩子拦截按键,允许按键有效和无效,可以记录按键,以及取消钩子。二、定义界面控件:label1 标记按键、button1 安装钩子按钮、 button2 取消钩子按钮三、源码1、主界面Form1using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Reflection;using System.Diagnostics;using Mic
2、rosoft.Win32;using WinformHook;namespace WinformHookpublic partial class Form1 : Form/钩子管理对象private KeyboardHookLib _keyboardHook = null;public Form1()InitializeComponent();private void button1_Click(object sender, EventArgs e)/安装钩子_keyboardHook = new KeyboardHookLib();_keyboardHook.InstallHook(this
3、.OnKeyPress);private void button2_Click(object sender, EventArgs e)/取消钩子if (_keyboardHook != null) _keyboardHook.UninstallHook();/ / 客户端键盘捕捉事件/ / 由Hook程序发送的按键信息 / 是否拦截 public void OnKeyPress(KeyboardHookLib.HookStruct hookStruct, out bool handle)handle = false; /预设置不拦截任何键if (hookStruct.vkCode = 91)
4、/截获左侧开始win键handle = true;if (hookStruct.vkCode = 92)/ 截获右侧开始win键handle = true;/截获Ctrl+Esc if (hookStruct.vkCode = (int)Keys.Escape /截获alt+f4 if (hookStruct.vkCode = (int)Keys.F4 /截获alt+tab if (hookStruct.vkCode = (int)Keys.Tab /截获F1 if (hookStruct.vkCode = (int)Keys.F1)handle = true;/截获Ctrl+Alt+Dele
5、te if (int)Control.ModifierKeys = (int)Keys.Control + (int)Keys.Alt + (int)Keys.Delete)handle = true;/如果键AZ if (hookStruct.vkCode = (int)Keys.A /键盘 /键盘处理事件委托,当捕获键盘输入时调用定义该委托的方法 private delegate int HookHandle(int nCode, int wParam, IntPtr lParam);/客户端键盘处理事件public delegate void ProcessKeyHandle(HookS
6、truct param, out bool handle);/接收SetWindowsHookEx返回值 private static int _hHookValue = 0;/勾子程序处理事件 private HookHandle _KeyBoardHookProcedure;/Hook结构 StructLayout(LayoutKind.Sequential)public class HookStructpublic int vkCode;public int scanCode;public int flags;public int time;public int dwExtraInfo;
7、/设置钩子 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);/调用下一个钩子
8、 DllImport(“user32.dll“)private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);/获取当前线程 DllImport(“kernel32.dll“)private static extern int GetCurrentThreadId();/Gets the main module for the associated process. DllImport(“kernel32.dll“)private static extern IntPtr G
9、etModuleHandle(string name);private IntPtr _hookWindowPtr = IntPtr.Zero;/构造器 public KeyboardHookLib() /外部调用的键盘处理事件 private static ProcessKeyHandle _clientMethod = null;/ / 安装勾子 / / 外部调用的键盘处理事件 public void InstallHook(ProcessKeyHandle clientMethod)_clientMethod = clientMethod;/ 安装键盘钩子 if (_hHookValue
10、 = 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 Unins
11、tallHook()if (_hHookValue != 0)bool ret = UnhookWindowsHookEx(_hHookValue);if (ret) _hHookValue = 0;/钩子事件内部调用调用方法转发到端应用 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; /表示拦截键盘退出 return CallNextHookEx(_hHookValue, nCode, wParam, lParam);