Maison >développement back-end >Tutoriel C#.Net >C# 键盘钩子
键盘钩子是一种可以监控键盘操作的指令,我们去钓鱼只要鱼儿上钩不管它怎么逃,只要掌控好钩子上的绳子总是可以找到这条鱼,键盘钩子是利用电脑一行行执行代码特性,在目的窗口处理键代码前拦截把某个指令替换为另外一种指令,然后再把消息传送给目的窗口这样一个周期下来,窗口程序会认为用户输入的就是现在的数值或者没有输入,不过键盘钩子在某些不法分子手里则成为了 盗号、监控密码 等违法操作。原型:HHOOK SetWindowsHookEx(int idHook, HOOKPROC lpfn, HINSTANCE hMod, DWORD dwThreadId);
// Win32 keyboard hook. public const int WH_KEYBOARD_LL = 13; public const int NULL = 0; public delegate int HookProc(int code, int wParam, KBDLLHOOKSTRUCT lParam); [DllImport("user32.dll", SetLastError = true)] public static extern int SetWindowsHookEx(int hookType, HookProc lpfn, int hMod, int dwThreadId); [DllImport("User32.dll", SetLastError = true)] public extern static int CallNextHookEx(int handle, int code, int wParam, KBDLLHOOKSTRUCT lParam); [StructLayout(LayoutKind.Sequential)] public class KBDLLHOOKSTRUCT { public uint vkCode; public uint scanCode; public KBDLLHOOKSTRUCT flags; public uint time; public UIntPtr dwExtraInfo; } [Flags] public enum KBDLLHOOKSTRUCT : uint { LLKHF_EXTENDED = 0x01, LLKHF_INJECTED = 0x10, LLKHF_ALTDOWN = 0x20, LLKHF_UP = 0x80, } public volatile int hHook; protected override void OnLoad(EventArgs e) { base.OnLoad(e); // 安装全局键盘钩子 if ((this.hHook = SetWindowsHookEx(WH_KEYBOARD_LL, this.KeyBoardProc, NULL, NULL)) == NULL) Console.WriteLine("Unable to establish a keyboard hook."); } protected int KeyBoardProc(int code, int wParam, KBDLLHOOKSTRUCT lParam) { if (lParam.vkCode == (int)Keys.A) return 1; // <span style="font-family: arial, 宋体, sans-serif;font-size:18px; line-height: 24px; text-indent: 28px;">返回1表示拦截消息,返回0表示放行</span> return CallNextHookEx(hHook, code, wParam, lParam); }
监控系统内所有进程键盘消息:
SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardProc, NULL, NULL)
<pre name="code" class="csharp"> // Win32 keyboard hook. public const int WH_KEYBOARD = 2; public const int NULL = 0; public delegate int HookProc(int code, int wParam, int lParam); [DllImport("kernel32.dll", SetLastError = true)] public static extern int GetCurrentThreadId(); [DllImport("user32.dll", SetLastError = true)] public static extern int SetWindowsHookEx(int hookType, HookProc lpfn, int hMod, int dwThreadId); [DllImport("User32.dll", SetLastError = true)] public extern static int CallNextHookEx(int handle, int code, int wParam, int lParam); public volatile int hHook; protected override void OnLoad(EventArgs e) { base.OnLoad(e); <pre name="code" class="csharp" style="line-height: 24px;font-size:18px;"> // 安装键盘钩子
if ((this.hHook = SetWindowsHookEx(WH_KEYBOARD, KeyBoardProc, NULL, GetCurrentThreadId())) == NULL) Console.WriteLine("Unable to establish a keyboard hook."); }
protected int KeyBoardQueue(int code, int wParam, int lParam) { if (wParam == (int)Keys.A) return 1; <span style="font-family: arial, 宋体, sans-serif;">// </span><span style="font-family: arial, 宋体, sans-serif;">返回1表示拦截消息,返回0表示放行</span> return CallNextHookEx(hHook, code, wParam, lParam); }
监控本进程的所有键盘消息:
SetWindowsHookEx(WH_KEYBOARD, KeyBoardProc, NULL, GetCurrentThreadId());
protected int KeyBoardProc(int code, int wParam, int lParam) { if (wParam == (int)Keys.A) return 1; return CallNextHookEx(hHook, code, wParam, lParam); }