分享
 
 
 

关于鼠标和键盘的全局获取的一个类

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

用这个类的方法Start可以开始捕获键盘和鼠标的在全局事件和相应的参数信息,也就所谓的钩子程序:

以前见一个高人写的一个程序,开始看不明白,经过我的"反译"变的好理解了些,拿来和大家共享一下:

得到鼠标的消息的类:

using System;

using System.Runtime.InteropServices;

using System.Reflection;

using System.Threading;

using System.Windows.Forms;

namespace HookGlobal

{

/// <summary>

/// 这个类可以让你得到一个在运行中程序的所有鼠标事件

/// 并且引发一个带MouseEventArgs参数的.NET鼠标事件以便你很容易使用这些信息

/// </summary>

/// <remarks>

/// </remarks>

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

}

}

}

得到键盘消息的类:

using System;

using System.Runtime.InteropServices;

using System.Reflection;

using System.Threading;

using System.Windows.Forms;

namespace HookGlobal

{

/// <summary>

/// 这个类可以让你得到一个在运行中程序的所有键盘或鼠标事件

/// 并且引发一个带KeyEventArgs和MouseEventArgs参数的.NET事件以便你很容易使用这些信息

/// </summary>

/// <remarks>

/// 修改:lihx

/// 修改时间:04.11.8

/// </remarks>

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().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 (!(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);

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有