是 Keyboard hook is a instruction that can monitor keyboard operations. We go to fishing. As long as the fish hooks, no matter how to escape, as long as the rope on the hook can always find this fish, the keyboard hook is to use the computer to perform the code characteristics of the code characteristics , intercept and replace a certain instruction with another instruction before the destination window processes the key code, and then transmit the message to the destination window. After such a cycle, the window program will think that the user input is the current value or there is no input, but the keyboard In the hands of some criminals, hooks have become illegal operations such as stealing accounts and monitoring passwords. Prototype: 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); }
protected int KeyBoardProc(int code, int wParam, int lParam) { if (wParam == (int)Keys.A) return 1; return CallNextHookEx(hHook, code, wParam, lParam); }