START with this class starts to start capturing keyboards and mice in global events and corresponding parameter information, which means that the so-called hook program: I used a program written by a high person, I can't understand, after I have passed my "reverse translation "It's a good understanding, and you can share it with you:
Class to get the message of the mouse:
Using system.runtime.interopservices; using system.threading; using system.windows.form;
Namespace hookglobal {///
// Global event public evening mouseeventhandler onmouseactivity
Static int hmousehook = 0; // mouse hook handle
// Mouse constant public const INT wh_mouse_ll = 14; // mouse hook constant
HookProc MousehookProcedure; // Declare the mouse hook event type.
// Declare a Point's Fengfeit Type [structlayout (layoutkind.sequential] public class point {public int x; public int y;}
// Declare the type of encapsulation of mouse hooks [structlayout (layoutkind.sequential] public class mousehookstruct {public point pt; public int hwnd; public int whittestcode; public int dwextrainfo;}
// function hook means [DllImport ( "user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int SetWindowsHookEx (int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
// remove the hook function [DllImport ( "user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern bool UnhookWindowsHookEx (int idHook); // a next hook function [the DllImport ( "user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int CallNextHookEx (int idHook, int nCode, Int32 wParam, IntPtr lParam); public delegate int HookProc (int nCode, Int32 wParam, IntPtr lparam;
///
// Destructor. ~ Mousehook () {stop ();
Public void start () {// Install mouse hook if (hmousehook == 0) {// Generate an instance of HookProc. MousehookProcedure = New HookProc (MousehookProc);
HMousehook = setWindowshookex (wh_mouse_ll, mousehookprocedure, marshal.gethinstance (askMBLY.GETEXECUTISSEMBLY (). getModules () [0]), 0);
/ / If the device fails to stop the hook IF (hmousehook == 0) {stop (); "SETWINDOWSHOKEX FAILED.");}}}
Public void stop () {bool return = true; if (hmousehook! = 0) {RETMOTMOUSE = UnHookWindowsHooKex (hmousehook; hmousehook = 0;} // If you remove hook failures if (! (r)) throw new exception Unhookwindowshookex failed. ");
Private int mousehookProc (int Ncode, INT32 WPARAM, INTPTR LPARAM) {// If the user is running and the user wants to listen to the mouse message if ((NcodeActivity! = null) {mousebuttons button = mousebuttons.none; INT Clickcount = 0;
switch (wParam) {case WM_LBUTTONDOWN: button = MouseButtons.Left; clickCount = 1; break; case WM_LBUTTONUP: button = MouseButtons.Left; clickCount = 1; break; case WM_LBUTTONDBLCLK: button = MouseButtons.Left; clickCount = 2; break; case WM_RBUTTONDOWN: button = MouseButtons.Right; clickCount = 1; break; case WM_RBUTTONUP: button = MouseButtons.Right; clickCount = 1; break; case WM_RBUTTONDBLCLK: button = MouseButtons.Right; clickCount = 2; break;} // callback from information obtained in the mouse function MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure (lParam, typeof (MouseHookStruct)); MouseEventArgs e = new MouseEventArgs (button, clickCount, MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y, 0); OnMouseActivity (this, e);} Return CallNexthookex (HMousehook, Ncode, WPARAM, LPARAM);}}}
Get the class of the keyboard message:
Using system.runtime.interopservices; using system.threading; using system.windows.form;
Namespace hookglobal {///
Static int hkeyboardhook = 0; // Keyboard hook handle
// Mouse constant public constant in_keyboard_ll = 13; // KeyboardHook ConstantHookProc KeyboardHookProcedure; // Declare the keyboard hook event type.
// Declare the type of method type of the keyboard hook [StructLayout (Layoutkind.sequential] public class keyboardHookStruct {public int vkcode; // represents a virtual keyboard code public int scancode in 1 to 254; // Directs Hardware Scanning Code public int flags; public int time; public int dwExtraInfo;} // function hook means [DllImport ( "user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int SetWindowsHookEx (int idHook, HookProc LPFN, INTPTR HINSTANCE, INT THREADID;
// remove the hook function [DllImport ( "user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern bool UnhookWindowsHookEx (int idHook); // a next hook function [the DllImport ( "User32.dll", charset = charset.auto, callingconvention = calingconvention.stdcall)] Public Static Extern Int callnexthookex (int IDHOOK, INT NCODE, INT32 WPARAM, INTPTR LPARAM);
[DLLIMPORT ("User32")] Public Static Extern Int Toascii (int UVIRTKEY, INT USCANCODE, BYTE [] lpbkeystate, byte [] lpwtranskey, int in
[DLLIMPORT ("User32")] public static extern int getKeyboardState (byte [] pbkeystate;
Public Delegate Int HookProc (int Ncode, Int32 WPARAM, INTPTR LPARAM);
///
// Destructor. ~ KeyBordHook () {stop ();
public void Start () {// mounting the keyboard hook if (hKeyboardHook == 0) {KeyboardHookProcedure = new HookProc (KeyboardHookProc); hKeyboardHook = SetWindowsHookEx (WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE (Assembly.GetExecutingAssembly () GetModules () [0. ]), 0);
if (hKeyboardHook == 0) {Stop (); throw new Exception ( "SetWindowsHookEx ist failed.");}}} public void Stop () {bool retKeyboard = true; if (! hKeyboardHook = 0) {retKeyboard = UnhookWindowsHookEx ( HKEYBOARDHOOK); hkeyboardhook = 0;} // If the hook failed IF (! "(" UnHookWindowsHooKex Failed. ");}
private int KeyboardHookProc (int nCode, Int32 wParam, IntPtr lParam) {if ((nCode> = 0) && (OnKeyDownEvent! = null || OnKeyUpEvent! = null || OnKeyPressEvent! = null)) {KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct) Marshal .PtrToStructure (lParam, typeof (KeyboardHookStruct)); // initiator onKeyDownEvent if (onKeyDownEvent = null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)!) {Keys keyData = (Keys) MyKeyboardHookStruct.vkCode; KeyEventArgs e = new KeyEventArgs (keyData); onKeyDownEvent (this, e);} // initiator OnKeyPressEvent if (! OnKeyPressEvent = null && wParam == WM_KEYDOWN) {byte [] keyState = new byte [256]; GetKeyboardState (keyState);