鼠标钩子(鼠标钩子是什么意思_内核hook鼠标)

发布时间:2025-12-10 19:18:43 浏览次数:8

鼠标钩子是什么意思_内核hook鼠标-鼠标钩子能不能检测出来

鼠标钩子是什么意思_内核hook鼠标MouseHook.csusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.InteropServices;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceCommon{publicclassMouseHook{

MouseHook.cs

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace Common{    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;        private const int WM_MOUSEWHEEL = 0x020A;        //private const int WM_VSCROLL = 0x0115;        public event MouseEventHandler OnMouseActivity;        public static int hMouseHook = 0;        public const int WH_MOUSE_LL = 14;//low level mouse event        public const int WH_MOUSE = 7;//normal level mouse event        //public const int WH_SYSMSGFILTER = 6;        HookProc MouseHookProcedure;        //Log _log = new Log("MouseHook", true, Log4netWrapper.Default);        [StructLayout(LayoutKind.Sequential)]        public class POINT        {            public int x;            public int y;        }        [StructLayout(LayoutKind.Sequential)]        public class MouseHookStruct        {            public POINT pt;            public int hWnd;            public int wHitTestCode;            public int dwExtraInfo;        }        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);        [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]        public static extern int GetLastError();        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]        private static extern IntPtr GetModuleHandle(string lpModuleName);        [DllImport("kernel32.dll")]        private static extern int GetCurrentThreadId();//获取在系统中的线程ID        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]        public static extern bool UnhookWindowsHookEx(int idHook);        [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);        public MouseHook()        {        }        ~MouseHook()        {            Stop();        }        public void Start()        {            if (hMouseHook == 0)            {                MouseHookProcedure = new HookProc(MouseHookProc);                //hMouseHook = SetWindowsHookEx(WH_SYSMSGFILTER, MouseHookProcedure, GetModuleHandle("user32"), 0);//第一个参数是WH_MOUSE_LL,表示捕获所有线程的鼠标消息,同时最后一个参数必须是0                hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, GetModuleHandle("user32"), 0);//第一个参数是WH_MOUSE_LL,表示捕获所有线程的鼠标消息,同时最后一个参数必须是0                //hMouseHook = SetWindowsHookEx(WH_MOUSE, MouseHookProcedure, GetModuleHandle("user32"), GetCurrentThreadId());//只捕获当前应用程序(当前线程)的鼠标消息,最后一个参数是当前线程id,使用GetCurrentThreadId()获得,一定不要使用托管线程id(Thread.CurrentThread.ManagedThreadId)。                if (hMouseHook == 0)                {                    int errorCode = GetLastError();                    //_log.E("SetWindowsHookEx failed.error code:" + errorCode);                    Stop();                }            }        }        public void Stop()        {            bool retMouse = true;            if (hMouseHook != 0)            {                retMouse = UnhookWindowsHookEx(hMouseHook);                hMouseHook = 0;            }            if (!(retMouse))            {                //_log.E("UnhookWindowsHookEx failed.");            }        }        private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)        {                  //只处理鼠标滚动的情况 WM_MOUSEWHEEL              if ((wParam == WM_MOUSEWHEEL || wParam == WM_LBUTTONUP) && (nCode >= 0) && (OnMouseActivity != 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;                    case WM_MOUSEWHEEL:                        button = MouseButtons.None;                        clickCount = 0;                        break;                }                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);        }    }}

是否还在为Ide开发工具频繁失效而烦恼,来吧关注以下公众号获取最新激活方式。亲测可用!

为防止网络爬虫,请关注公众号回复”口令”

激活idea 激活CLion DataGrip DataSpell dotCover dotMemory dotTrace GoLand PhpStorm PyCharm ReSharper ReShaC++ Rider RubyMine WebStorm 全家桶 刷新

【正版授权,激活自己账号】:Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】:官方授权 正版激活 自己使用,支持Jetbrains家族下所有IDE…

使用HOOK

 //启用钩子 hook = new MouseHook(); hook.OnMouseActivity += Hook_OnMouseActivity; hook.Start(); //注销钩子 hook.Stop();

钩子子程序

private static void Hook_OnMouseActivity(object sender, MouseEventArgs e)        {            try            {                    if (e.Button == System.Windows.Forms.MouseButtons.Left)                    {                        //业务处理                    }            }            catch (Exception ex)            {                            }        }
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477