C#全局鼠标键盘Hook

原文出自:http://www.cnblogs.com/iEgrhn/archive/2008/02/17/1071392.html

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace DCIEngine.FrameWork.Snap
{
    ///   <summary>
    ///   这个类可以让你得到一个在运行中程序的所有键盘或鼠标事件
    ///   并且引发一个带KeyEventArgs参数的.NET事件以便你很容易使用这些信息
    ///   </summary>

    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;

        //全局的事件
        public event KeyEventHandler OnKeyDownEvent;
        public event KeyEventHandler OnKeyUpEvent;
        public event KeyPressEventHandler OnKeyPressEvent;
        static int hKeyboardHook = 0;   //键盘钩子句柄
        //鼠标常量
        public const int WH_KEYBOARD_LL = 13;   //keyboard   hook   constant
        HookProc KeyboardHookProcedure;   //声明键盘钩子事件类型.
        //声明键盘钩子的封送结构类型
        [StructLayout(LayoutKind.Sequential)]
        public class KeyboardHookStruct
        {
            public int vkCode;   //表示一个在1到254间的虚似键盘码
            public int scanCode;   //表示硬件扫描码
            public int flags;
            public int time;
            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("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);
        [DllImport("user32 ")]
        public static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState);
        [DllImport("user32 ")]
        public static extern int GetKeyboardState(byte[] pbKeyState);
        public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
        ///   <summary>
        ///   墨认的构造函数构造当前类的实例并自动的运行起来.
        ///   </summary>
        public KeyBordHook()
        {
            Start();
        }
        //析构函数.
        ~KeyBordHook()
        {
            Stop();
        }
        public void Start()
        {
            //安装键盘钩子
            if (hKeyboardHook == 0)
            {
                KeyboardHookProcedure = new HookProc(KeyboardHookProc);
                hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule), 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 (!(retKeyboard)) throw new Exception("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));
                //引发OnKeyDownEvent
                if (OnKeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
                {
                    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
                    KeyEventArgs e = new KeyEventArgs(keyData);
                    OnKeyDownEvent(this, e);
                }

                //引发OnKeyPressEvent
                if (OnKeyPressEvent != null && wParam == WM_KEYDOWN)
                {
                    byte[] keyState = new byte[256];
                    GetKeyboardState(keyState);
                    byte[] inBuffer = new byte[2];
                    if (ToAscii(MyKeyboardHookStruct.vkCode,
                      MyKeyboardHookStruct.scanCode,
                      keyState,
                      inBuffer,
                      MyKeyboardHookStruct.flags) == 1)
                    {
                        KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);
                        OnKeyPressEvent(this, e);
                    }
                }

                //引发OnKeyUpEvent
                if (OnKeyUpEvent != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
                {
                    Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
                    KeyEventArgs e = new KeyEventArgs(keyData);
                    OnKeyUpEvent(this, e);
                }
            }
            return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
        }
    }

    ///   <summary>
    ///   这个类可以让你得到一个在运行中程序的所有鼠标事件
    ///   并且引发一个带MouseEventArgs参数的.NET鼠标事件以便你很容易使用这些信息
    ///   </summary>
    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;
        //全局的事件
        public event MouseEventHandler OnMouseActivity;
        static int hMouseHook = 0;   //鼠标钩子句柄
        //鼠标常量
        public const int WH_MOUSE_LL = 14;   //mouse   hook   constant
        HookProc MouseHookProcedure;   //声明鼠标钩子事件类型.
        //声明一个Point的封送类型
        [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("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);
        ///   <summary>
        ///   墨认的构造函数构造当前类的实例.
        ///   </summary>
        public MouseHook()
        {
            //Start();
        }
        //析构函数.
        ~MouseHook()
        {
            Stop();
        }
        public void Start()
        {
            //安装鼠标钩子
            if (hMouseHook == 0)
            {
                //生成一个HookProc的实例.
                MouseHookProcedure = new HookProc(MouseHookProc);
                hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
                //如果装置失败停止钩子
                if (hMouseHook == 0)
                {
                    Stop();
                    throw new Exception("SetWindowsHookEx failed. ");
                }
            }
        }
        public void Stop()
        {
            bool retMouse = true;
            if (hMouseHook != 0)
            {
                retMouse = UnhookWindowsHookEx(hMouseHook);
                hMouseHook = 0;
            }

            //如果卸下钩子失败
            if (!(retMouse)) throw new Exception("UnhookWindowsHookEx   failed. ");
        }
        private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            //如果正常运行并且用户要监听鼠标的消息
            if ((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;
                }
                //从回调函数中得到鼠标的信息
                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);
        }
    }
}

在VS中直接运行,HOOK将会失败。

必须生成Release然后直接运行EXE才能生效。。

如果失败的话,可以把回调寒函数设置成静态的

时间: 2024-10-27 08:24:08

C#全局鼠标键盘Hook的相关文章

如何在C#中使用全局鼠标、键盘Hook

今天,有个同事问我,怎样在C#中使用全局钩子?以前写的全局钩子都是用unmanaged C或C++写个DLL来实现,可大家都知道,C#是基于.Net Framework的,是managed,怎么实现全局钩子呢?于是开始到网上搜索,好不容易找到一篇,318804 - HOW TO: Set a Windows Hook in Visual C# .NET,里面详细的说明了如何使用鼠标钩子捕获鼠标的移动等,可是,它只能在Application里起作用,出了Application就没用了,就是说它还是

hook 鼠标键盘消息实例分析

1.木马控制及通信方法包括:双管道,端口重用,反弹技术,Hook技术,今天重点引用介绍一下hook的用法,hook信息后可以将结果发送到hacker邮箱等,实现攻击的目的. 转自:http://hi.baidu.com/mousetopshow/item/a951102d679f6e8f9c63d1b0 钩子能截获系统并得理发送给其它应用程序的消息,能完成一般程序无法完成的功能.掌握钩子的编程方法是很有必要的 钩子分类 : 1.WH_CALLWNDPROC和WH_CALLWNDPROCRET:

模拟鼠标键盘操作,含硬件模拟技术[转载]

键盘是我们使用计算机的一个很重要的输入设备了,即使在鼠标大行其道的今天,很多程序依然离不开键盘来操作.但是有时候,一些重复性的,很繁琐的键盘操作总会让人疲惫,于是就有了用程序来代替人们按键的方法,这样可以把很多重复性的键盘操作交给程序来模拟,省了很多精力,按键精灵就是这样的一个软件.那么我们怎样才能用VB来写一个程序,达到与按键精灵类似的功能呢?那就让我们来先了解一下windows中响应键盘事件的机制.    当用户按下键盘上的一个键时,键盘内的芯片会检测到这个动作,并把这个信号传送到计算机.如

C#鼠标键盘钩子

using System;using System.Collections.Generic; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace ICS.Common { /// <summary> /// 这个类可以让你得到一个在运行中程序的所有键盘或鼠标事件 /// 并且引发一个带KeyEventArgs

c#全局鼠标事件以及鼠标事件模拟

最近在编写Max插件时,其主容器FlowLayoutPanel由于隐藏了滚动条,要实现按住鼠标中键上下拖动的功能,因此尝试了全局鼠标事件.以及鼠标勾子,可惜由于Max不争气?都未能实现,于是代码报废,故将其分享于此. 一.全局鼠标事件,首先构建鼠标事件处理器 public delegate void MouseMovedEvent(); public delegate void MouseMDownEvent(); public delegate void MouseMUpEvent(); pu

常规鼠标键盘钩子.映像劫持.开机自启动

using System;using System.Collections.Generic;using System.IO;using System.Windows.Forms;using System.Runtime.InteropServices;using System.Reflection; namespace HookTest{ /* 注意: 如果运行中出现SetWindowsHookEx的返回值为0,这是因为.net 调试模式的问题,具体的做法是禁用宿主进程,在 Visual Stu

使用c#创建一个可以监视全局鼠标位置的程序

首先要在焦点不在当前应用程序中也要获得鼠标位置,我们就需要使用全局鼠标钩子 我们先新建一个类库GlobalMouseHook, 然后导入命名空间:System.Windows.Forms;System.Runtime.InteropServices; 将类重命名为MouseHook,其代码如下: [c-sharp] view plaincopy using System.Windows.Forms; using System.Runtime.InteropServices; namespace 

python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为

1.参考 autopy (实践见最后一章节) 用Python制作游戏外挂(上) AutoPy Introduction and Tutorial autopy.mouse.smooth_move(1, 1) 可以实现平滑移动 autopy - API Reference pip install PyUserInput SavinaRoja/PyUserInput [python3.5][PyUserInput]模拟鼠标和键盘模拟 Python-模拟鼠标键盘动作 autoit selenium借助

多电脑同局域网,同一套鼠标键盘,跨屏操作利器。

妈妈再也不担心我拿错键盘鼠标了. Input Director是款Windows下的一套鼠标键盘控制多台电脑工具,使用户可以通过连接在一台计算机上的一套键盘鼠标,轻松控制多台电脑.对于经常在不同电脑之间经常切换的用户来说,非常实用.我也不用为了测试一个软件,在本本和测试机之间来回切换.不过为了使用方便,最好是能将各个系统的显示器排成一排.  除了支持多个系统的切换和操作,Input Director同样支持“共享”剪贴板,在一个计算机上复制数据,可以切换到其他计算机上粘贴使用.不过Windows