C # Keyboard Hooks

Source: Internet
Author: User
Keyboard Hook is an instruction that can monitor the operation of the keyboard, we go fishing as long as the fish hook no matter how it escaped, as long as the control of the hook on the rope can always find the fish, keyboard hooks is the use of computer line to execute code characteristics, in the purpose of the window processing key code before a command to replace a different command, And then send the message to the destination window such a cycle down, the window program will assume that the user input is the current value or no input, but the keyboard hook in some criminals in the hand has become the theft, monitoring password and other illegal operations. Prototypes: 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 h Mod, int dwthreadid); [DllImport ("User32.dll", SetLastError = True)]public extern static int CallNextHookEx (int handle, int code, int wParam, KB Dllhookstruct 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); Install the global keyboard hook 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) Retu RN 1;  //returns 1 for intercept message, 0 for releaseReturn CallNextHookEx (Hhook, Code, WParam, LParam);}

Monitor all process keyboard messages in the system:
SetWindowsHookEx (WH_KEYBOARD_LL, keyboardproc, NULL, NULL)

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 LPF         n, int hmod, int dwthreadid); [DllImport ("User32.dll", SetLastError = true)] public extern static int CallNextHookEx (int handle, int code, int WP         Aram, int lParam);         public volatile int hhook; protected override void OnLoad (EventArgs e) {base. OnLoad (e);
            Installing the keyboard hooks
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)         returns 1 for intercept message , return 0 means release    return CallNextHookEx (Hhook, Code, WParam, LParam);}

Monitor all keyboard messages for this process:

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);   }
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.