*封装的hook类:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices; //必须引用
using System.Reflection; //必须引用
namespace KeyboardHook
{
class Hocy_Hook
{
#region私有常量
///<summary>
///按键状态数组
///</summary>
private readonly byte[] m_KeyState = new byte[ 256 ];
private string flags;
//flag=0 正常 flag=1 监控状态 flag=2 屏蔽键盘//
#endregion私有常量
#region私有变量
///<summary>
///鼠标钩子句柄
///</summary>
private IntPtr m_pMouseHook = IntPtr.Zero;
///<summary>
///键盘钩子句柄
///</summary>
private IntPtr m_pKeyboardHook = IntPtr.Zero;
///<summary>
///鼠标钩子委托实例
///</summary>
///<remarks>
///不要试图省略此变量,否则将会导致
///激活CallbackOnCollectedDelegate 托管调试助手(MDA)。
///详细请参见MSDN中关于CallbackOnCollectedDelegate 的描述
///</remarks>
private HookProc m_MouseHookProcedure;
///<summary>
///键盘钩子委托实例
///</summary>
///<remarks>
///不要试图省略此变量,否则将会导致
///激活CallbackOnCollectedDelegate 托管调试助手(MDA)。
///详细请参见MSDN中关于CallbackOnCollectedDelegate 的描述
///</remarks>
private HookProc m_KeyboardHookProcedure;
// 添加
public event MouseEventHandler OnMouseActivity;
private const byte VK_SHIFT = 0x10 ;
private const byte VK_CAPITAL = 0x14;
private const byte VK_NUMLOCK = 0x90;
#endregion私有变量
#region事件定义
///<summary>
///鼠标更新事件
///</summary>
///<remarks>当鼠标移动或者滚轮滚动时触发</remarks>
public event MouseUpdateEventHandler OnMouseUpdate;
///<summary>
///按键按下事件
///</summary>
public event KeyEventHandler OnKeyDown;
///<summary>
///按键按下并释放事件
///</summary>
public event KeyPressEventHandler OnKeyPress;
///<summary>
///按键释放事件
///</summary>
public event KeyEventHandler OnKeyUp;
#endregion事件定义
#region私有方法
///<summary>
///鼠标钩子处理函数
///</summary>
///<param name="nCode"></param>
///<param name="wParam"></param>
///<param name="lParam"></param>
///<returns>鼠标钩子处理函数</returns>
private int MouseHookProc( int nCode, Int32 wParam, IntPtr lParam )
{
if ((nCode >= 0) && (OnMouseActivity != null))
{
//Marshall the data from callback.
MouseHookStruct mouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
//detect button clicked
MouseButtons button = MouseButtons.None;
short mouseDelta = 0;
switch (wParam)
{
case (int)WM_MOUSE.WM_LBUTTONDOWN:
//case WM_LBUTTONUP:
//case WM_LBUTTONDBLCLK:
button = MouseButtons.Left;
break;
case (int)WM_MOUSE.WM_RBUTTONDOWN:
//case WM_RBUTTONUP:
//case WM_RBUTTONDBLCLK:
button = MouseButtons.Right;
break;
case (int)WM_MOUSE.WM_MOUSEWHEEL:
//If the message is WM_MOUSEWHEEL, the high-order word of mouseData member is the wheel delta.
//One wheel click is defined as WHEEL_DELTA, which is 120.
//(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
mouseDelta = (short)((mouseHookStruct.MouseData>> 16) & 0xffff);
//TODO: X BUTTONS (I havent them so was unable to test)
//If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,
//or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released,
//and the low-order word is reserved. This value can be one or more of the following values.
//Otherwise, mouseData is not used.
break;
}
//double clicks
int clickCount = 0;
if (button != MouseButtons.None)
if (wParam == (int)WM_MOUSE.WM_LBUTTONDBLCLK || wParam == (int)WM_MOUSE.WM_RBUTTONDBLCLK) clickCount = 2;
else clickCount = 1;
//generate event
MouseEventArgs e = new MouseEventArgs(
button,
clickCount,
mouseHookStruct.Point.X,
mouseHookStruct.Point.Y,
mouseDelta);
//raise it
OnMouseActivity(this, e);
}
//*
return Win32API.CallNextHookEx( this.m_pMouseHook, nCode, wParam, lParam );
}
///<summary>
///键盘钩子处理函数
///</summary>
///<param name="nCode"></param>
///<param name="wParam"></param>
///<param name="lParam"></param>
///<returns></returns>
///<remarks></remarks>
private int KeyboardHookProc( int nCode, Int32 wParam, IntPtr lParam )
{
switch (flags)
{
case "2":
return 1;
break;
case "1":
break;
}
bool handled = false;
//it was ok and someone listens to events
if ((nCode >= 0) && (this.OnKeyDown != null || this.OnKeyUp!= null || this.OnKeyPress!= null))
{
//read structure KeyboardHookStruct at lParam
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
//raise KeyDown
if (this.OnKeyDown != null && (wParam == (int)WM_KEYBOARD.WM_KEYDOWN || wParam == (int)WM_KEYBOARD.WM_SYSKEYDOWN))
{
Keys keyData = (Keys)MyKeyboardHookStruct.VKCode;
KeyEventArgs e = new KeyEventArgs(keyData);
this.OnKeyDown(this, e);
handled = handled || e.Handled;
}
// raise KeyPress
if (this.OnKeyPress != null && wParam == (int)WM_KEYBOARD.WM_KEYDOWN)
{
bool isDownShift, isDownCapslock;
try
{
isDownShift = ((Win32API.GetKeyStates(VK_SHIFT) & 0x80) == 0x80 ? true : false);
isDownCapslock = (Win32API.GetKeyStates(VK_CAPITAL) != 0 ? true : false);
}
catch
{
isDownCapslock = false;
做钩子可以参考一下
需积分: 0 189 浏览量
2008-05-05
14:09:41
上传
评论
收藏 3KB RAR 举报
meng3147
- 粉丝: 0
- 资源: 1
最新资源
- Python闭包:深入理解与应用场景解析.zip
- Win64OpenSSL-3-3-0.exe
- 课高分程设计-基于C++实现的民航飞行与地图简易管理系统-南京航空航天大学
- 航天器遥测数据故障检测系统python源码+文档说明+数据库(课程设计)
- 北京航空航天大学操作系统课设+ppt+实验报告
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
- 基于单片机的风力发电机转速控制源码
- 基于C++实现的风力发电气动平衡监测系统+源代码+测量数据(高分课程设计)
- 毕业设计- 基于STM32F103C8T6 单片机,物联网技术的太阳能发电装置+源代码+文档说明+架构图+界面截图
- 基于 LSTM(长短期记忆)(即改进的循环神经网络)预测风力发电厂中风力涡轮机产生的功率+源代码+文档说明
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0