/// <summary> /// Override for the generic hook's invoked event to /// convert to a strongly typed MouseHookEvent: /// </summary> /// <param name="e">Generic Hook event argument details</param> protected override void OnHookInvoked(ref HookEventArgs e) { // Convert into mouse details: MOUSEHOOKSTRUCT mhs = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure( e.lParam, typeof(MOUSEHOOKSTRUCT)); MouseHookEventArgs mhe = new MouseHookEventArgs( e.wParam, mhs); OnMouseHookEvent(ref mhe); }
/// <summary> /// Constructs a new MouseHookEvent /// </summary> /// <param name="wParam">The <c>wParam</c> (Message code) for the /// Mouse Hook event</param> /// <param name="mhs">The <c>MOUSEHOOKEVENT</c> structure /// for the hook event.</param> public MouseHookEventArgs( IntPtr wParam, MOUSEHOOKSTRUCT mhs ) { switch ((int)wParam) { case WM_MOUSEMOVE: eventType = MouseHookEventType.MouseMove; // we could check if we're over a non-client // area here etc button = MouseButtons.None; break; case WM_LBUTTONDOWN: eventType = MouseHookEventType.MouseDown; button = MouseButtons.Left; eventLocation = MouseHookEventLocation.Client; break; case WM_LBUTTONUP: eventType = MouseHookEventType.MouseUp; button = MouseButtons.Left; eventLocation = MouseHookEventLocation.Client; break; case WM_LBUTTONDBLCLK: eventType = MouseHookEventType.DblClick; button = MouseButtons.Left; eventLocation = MouseHookEventLocation.Client; break; case WM_MBUTTONDOWN: eventType = MouseHookEventType.MouseDown; button = MouseButtons.Middle; eventLocation = MouseHookEventLocation.Client; break; case WM_MBUTTONUP: eventType = MouseHookEventType.MouseUp; button = MouseButtons.Middle; eventLocation = MouseHookEventLocation.Client; break; case WM_MBUTTONDBLCLK: eventType = MouseHookEventType.DblClick; button = MouseButtons.Middle; eventLocation = MouseHookEventLocation.Client; break; case WM_RBUTTONDOWN: eventType = MouseHookEventType.MouseDown; button = MouseButtons.Right; eventLocation = MouseHookEventLocation.Client; break; case WM_RBUTTONUP: eventType = MouseHookEventType.MouseUp; button = MouseButtons.Right; eventLocation = MouseHookEventLocation.Client; break; case WM_RBUTTONDBLCLK: eventType = MouseHookEventType.DblClick; button = MouseButtons.Right; eventLocation = MouseHookEventLocation.Client; break; case WM_NCLBUTTONDOWN: eventType = MouseHookEventType.MouseDown; button = MouseButtons.Left; eventLocation = MouseHookEventLocation.NonClient; break; case WM_NCLBUTTONUP: eventType = MouseHookEventType.MouseUp; button = MouseButtons.Left; eventLocation = MouseHookEventLocation.NonClient; break; case WM_NCLBUTTONDBLCLK: eventType = MouseHookEventType.DblClick; button = MouseButtons.Left; eventLocation = MouseHookEventLocation.NonClient; break; case WM_NCMBUTTONDOWN: eventType = MouseHookEventType.MouseDown; button = MouseButtons.Middle; eventLocation = MouseHookEventLocation.NonClient; break; case WM_NCMBUTTONUP: eventType = MouseHookEventType.MouseUp; button = MouseButtons.Middle; eventLocation = MouseHookEventLocation.NonClient; break; case WM_NCMBUTTONDBLCLK: eventType = MouseHookEventType.DblClick; button = MouseButtons.Middle; eventLocation = MouseHookEventLocation.NonClient; break; case WM_NCRBUTTONDOWN: eventType = MouseHookEventType.MouseDown; button = MouseButtons.Right; eventLocation = MouseHookEventLocation.NonClient; break; case WM_NCRBUTTONUP: eventType = MouseHookEventType.MouseUp; button = MouseButtons.Right; eventLocation = MouseHookEventLocation.NonClient; break; case WM_NCRBUTTONDBLCLK: eventType = MouseHookEventType.DblClick; button = MouseButtons.Right; eventLocation = MouseHookEventLocation.NonClient; break; } x = mhs.x; y = mhs.y; handle = mhs.handle; hitTestCode = mhs.wHitTestCode; extraData = mhs.dwExtraInfo; }