




已阅读5页,还剩18页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
using System;using System.ComponentModel;using System.Reflection;using System.Runtime.InteropServices;using System.Windows.Forms;namespace UserActivityMonitor / / This class monitors all mouse activities globally (also outside of the application) / and provides appropriate events. / public static class HookManager / / Occurs when the mouse pointer is moved. / public static event MouseEventHandler MouseMove add EnsureSubscribedToGlobalMouseEvents(); s_MouseMove += value; remove s_MouseMove -= value; TryUnsubscribeFromGlobalMouseEvents(); / / Occurs when a click was performed by the mouse. / public static event MouseEventHandler MouseClick add EnsureSubscribedToGlobalMouseEvents(); s_MouseClick += value; remove s_MouseClick -= value; TryUnsubscribeFromGlobalMouseEvents(); / / Occurs when the mouse a mouse button is pressed. / public static event MouseEventHandler MouseDown add EnsureSubscribedToGlobalMouseEvents(); s_MouseDown += value; remove s_MouseDown -= value; TryUnsubscribeFromGlobalMouseEvents(); / / Occurs when a mouse button is released. / public static event MouseEventHandler MouseUp add EnsureSubscribedToGlobalMouseEvents(); s_MouseUp += value; remove s_MouseUp -= value; TryUnsubscribeFromGlobalMouseEvents(); / / Occurs when the mouse wheel moves. / public static event MouseEventHandler MouseWheel add EnsureSubscribedToGlobalMouseEvents(); s_MouseWheel += value; remove s_MouseWheel -= value; TryUnsubscribeFromGlobalMouseEvents(); / / Occurs when a double clicked was performed by the mouse. / public static event MouseEventHandler MouseDoubleClick add EnsureSubscribedToGlobalMouseEvents(); if (s_MouseDoubleClick = null) s_DoubleClickTimer = new Timer Interval = GetDoubleClickTime(), Enabled = false ; s_DoubleClickTimer.Tick += DoubleClickTimeElapsed; MouseUp += OnMouseUp; s_MouseDoubleClick += value; remove if (s_MouseDoubleClick != null) s_MouseDoubleClick -= value; if (s_MouseDoubleClick = null) MouseUp -= OnMouseUp; s_DoubleClickTimer.Tick -= DoubleClickTimeElapsed; s_DoubleClickTimer = null; TryUnsubscribeFromGlobalMouseEvents(); / / Occurs when a key is released. / public static event KeyEventHandler KeyUp add EnsureSubscribedToGlobalKeyboardEvents(); s_KeyUp += value; remove s_KeyUp -= value; TryUnsubscribeFromGlobalKeyboardEvents(); / / Occurs when a key is preseed. / public static event KeyEventHandler KeyDown add EnsureSubscribedToGlobalKeyboardEvents(); s_KeyDown += value; remove s_KeyDown -= value; TryUnsubscribeFromGlobalKeyboardEvents(); / / Occurs when a key is pressed. / / / Key events occur in the following order: / / KeyDown / KeyPress / KeyUp / /The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events. /Use the KeyChar property to sample keystrokes at run time and to consume or modify a subset of common keystrokes. /To handle keyboard events only in your application and not enable other applications to receive keyboard events, / set the KeyPressEventArgs.Handled property in your forms KeyPress event-handling method to true. / public static event KeyPressEventHandler KeyPress add EnsureSubscribedToGlobalKeyboardEvents(); s_KeyPress += value; remove s_KeyPress -= value; TryUnsubscribeFromGlobalKeyboardEvents(); private static event MouseEventHandler s_MouseMove; private static event MouseEventHandler s_MouseClick; private static event MouseEventHandler s_MouseDown; private static event MouseEventHandler s_MouseUp; private static event MouseEventHandler s_MouseWheel; private static event MouseEventHandler s_MouseDoubleClick; private static MouseButtons s_PrevClickedButton; private static Timer s_DoubleClickTimer; private static void DoubleClickTimeElapsed(object sender, EventArgs e) s_DoubleClickTimer.Enabled = false; s_PrevClickedButton = MouseButtons.None; private static void OnMouseUp(object sender, MouseEventArgs e) if (e.Clicks = 0) var mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct); var button = MouseButtons.None; short mouseDelta = 0; var clickCount = 0; var mouseDown = false; var mouseUp = false; switch (wParam) case WM_LBUTTONDOWN: mouseDown = true; button = MouseButtons.Left; clickCount = 1; break; case WM_LBUTTONUP: mouseUp = true; button = MouseButtons.Left; clickCount = 1; break; case WM_LBUTTONDBLCLK: button = MouseButtons.Left; clickCount = 2; break; case WM_RBUTTONDOWN: mouseDown = true; button = MouseButtons.Right; clickCount = 1; break; case WM_RBUTTONUP: mouseUp = true; button = MouseButtons.Right; clickCount = 1; break; case WM_RBUTTONDBLCLK: button = MouseButtons.Right; clickCount = 2; break; case WM_MOUSEWHEEL: mouseDelta = (short)(mouseHookStruct.MouseData 16) & 0xffff); break; var e = new MouseEventExtArgs( button, clickCount, mouseHookStruct.Point.X, mouseHookStruct.Point.Y, mouseDelta); if (s_MouseUp != null & mouseUp) s_MouseUp.Invoke(null, e); if (s_MouseDown != null & mouseDown) s_MouseDown.Invoke(null, e); if (s_MouseClick != null & clickCount 0) s_MouseClick.Invoke(null, e); if (s_MouseDoubleClick != null & clickCount = 2) s_MouseDoubleClick.Invoke(null, e); if (s_MouseWheel != null & mouseDelta != 0) s_MouseWheel.Invoke(null, e); if (s_MouseMove != null) & (m_OldX != mouseHookStruct.Point.X | m_OldY != mouseHookStruct.Point.Y) m_OldX = mouseHookStruct.Point.X; m_OldY = mouseHookStruct.Point.Y; if (s_MouseMove != null) s_MouseMove.Invoke(null, e); if (e.Handled) return -1; return CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam); private static void EnsureSubscribedToGlobalMouseEvents() if (s_MouseHookHandle != 0) return; s_MouseDelegate = MouseHookProc; s_MouseHookHandle = SetWindowsHookEx( WH_MOUSE_LL, s_MouseDelegate, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()0), 0); if (s_MouseHookHandle != 0) return; throw new Win32Exception(Marshal.GetLastWin32Error(); private static void TryUnsubscribeFromGlobalMouseEvents() if (s_MouseClick = null & s_MouseDown = null & s_MouseMove = null & s_MouseUp = null & s_MouseWheel = null) ForceUnsunscribeFromGlobalMouseEvents(); private static void ForceUnsunscribeFromGlobalMouseEvents() if (s_MouseHookHandle = 0) return; var result = UnhookWindowsHookEx(s_MouseHookHandle); s_MouseHookHandle = 0; s_MouseDelegate = null; if (result != 0) return; throw new Win32Exception(Marshal.GetLastWin32Error(); private static HookProc s_KeyboardDelegate; private static int s_KeyboardHookHandle; private static int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) var handled = false; if (nCode = 0) var MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct); if (s_KeyDown != null & (wParam = WM_KEYDOWN | wParam = WM_SYSKEYDOWN) var keyData = (Keys)MyKeyboardHookStruct.VirtualKeyCode; var e = new KeyEventArgs(keyData); s_KeyDown.Invoke(null, e); handled = e.Handled; if (s_KeyPress != null & wParam = WM_KEYDOWN) var isDownShift = (GetKeyState(VK_SHIFT) & 0x80) = 0x80); var isDownCapslock = (GetKeyState(VK_CAPITAL) != 0); var keyState = new byte256; GetKeyboardState(keyState); var inBuffer = new byte2; if (ToAscii(MyKeyboardHookStruct.VirtualKeyCode, MyKeyboardHookStruct.ScanCode, keyState, inBuffer, MyKeyboardHookStruct.Flags) = 1) var key = (char)inBuffer0; if (isDownCapslock isDownShift) & Char.IsLetter(key) key = Char.ToUpper(key); var e = new KeyPressEventArgs(key); s_KeyPress.Invoke(null, e); handled = handled | e.Handled; if (s_KeyUp != null & (wParam = WM_KEYUP | wParam = WM_SYSKEYUP) var keyData = (Keys)MyKeyboardHookStruct.VirtualKeyCode; var e = new KeyEventArgs(keyData); s_KeyUp.Invoke(null, e); handled = handled | e.Handled; if (handled) return -1; return CallNextHookEx(s_KeyboardHookHandle, nCode, wParam, lParam); private static void EnsureSubscribedToGlobalKeyboardEvents() if (s_KeyboardHo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 人才日活动策划方案模板
- 东莞入企咨询方案
- 传媒艺考活动策划方案
- 水产养殖行业技术规范总结
- 软件著作权转让协议
- 2025结构工程师检测卷(历年真题)附答案详解
- 2025国家统一法律职业资格考试考试真题及答案
- 中考语文小说阅读赏练-梁晓声小说(含解析)
- 诚信与谎言话题作文(13篇)
- 2025计算机一级考前冲刺练习试题及完整答案详解(夺冠系列)
- 2025-2026学年地质版(2024)小学体育与健康二年级(全一册)教学设计(附目录P173)
- 茶百道培训课件
- 2025至2030年中国制药装备行业市场全景分析及投资前景展望报告
- 2025北京京剧院招聘工作人员10人考试备考题库及答案解析
- 检修现场管理培训课件
- 2025年食品安全人员在线考试试题及答案
- 多重耐药菌感染患者的护理LP
- 信息网络安全考题「附答案」
- 2025年度国务院国资委干部教育培训中心招聘(2人)笔试备考试题及答案详解(历年真题)
- 2025司法协理员考试模拟题及答案
- 消防设备设施操作讲解培训课件P
评论
0/150
提交评论