A class regarding global acquisition of mouse and keyboard

xiaoxiao2021-03-06  74

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 {///

/// This class allows you to get all mouse events / / / / / / / in the running program and trigger a .NET mouse event with a mouseeventargs parameter so you easily use this information // / /// /// public class MouseHook {private const int WM_MOUSEMOVE = 0x200; private const int WM_LBUTTONDOWN = 0x201; private const int WM_RBUTTONDOWN = 0x204; private const int WM_MBUTTONDOWN = 0x207; private const int WM_LBUTTONUP = 0x202; private const int WM_RBUTTONUP = 0x205; private const int WM_MBUTTONUP = 0x208; private const int WM_LBUTTONDBLCLK = 0x203; private const int WM_RBUTTONDBLCLK = 0x206; private const int WM_MBUTTONDBLCLK = 0x209;

// 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;

///

/// The constructor constructor constructs an example of the current class. /// public mousehook () {// start ();}

// 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 {///

/// This class allows you to get all keyboards or mouse events in the running program / // and triggered a .NET event with KeyEventArgs and MouseEventArgs parameters so you can easily use these Information /// /// /// Modification: lihx /// Modification time: 04.11.8 /// public class keybordhook {private const INT WM_KEYDOWN = 0x100; private const Int WM_KEYUP = 0x101; private const int WM_SYSKEYDOWN = 0x104; private const int WM_SYSKEYUP = 0x105; // global event public event KeyEventHandler onKeyDownEvent; public event KeyEventHandler OnKeyUpEvent; public event KeyPressEventHandler OnKeyPressEvent;

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);

///

/// The constructor constructor constructs an instance of the current class and runs automatically. /// public keybordhook () {start ();}

// 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);

转载请注明原文地址:https://www.9cbs.com/read-92986.html

New Post(0)