/// <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 ) && ( this.OnMouseUpdate != null ) * && ( wParam == ( int )WM_MOUSE.WM_MOUSEMOVE || wParam == ( int )WM_MOUSE.WM_MOUSEWHEEL ) ) * { * MouseHookStruct MouseInfo = ( MouseHookStruct )Marshal.PtrToStructure( lParam, typeof( MouseHookStruct ) ); * this.OnMouseUpdate( MouseInfo.Point.X, MouseInfo.Point.Y ); * }*/ //* 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; isDownShift = false; } byte[] keyState = new byte[256]; Win32API.GetKeyboardState(keyState); byte[] inBuffer = new byte[2]; if (Win32API.ToAscii(MyKeyboardHookStruct.VKCode, MyKeyboardHookStruct.ScanCode, keyState, inBuffer, MyKeyboardHookStruct.Flags) == 1) { char key = (char)inBuffer[0]; if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key)) { key = Char.ToUpper(key); } KeyPressEventArgs e = new KeyPressEventArgs(key); this.OnKeyPress(this, e); handled = handled || e.Handled; } } // raise KeyUp if (this.OnKeyUp != null && (wParam == (int)WM_KEYBOARD.WM_KEYUP || wParam == (int)WM_KEYBOARD.WM_SYSKEYUP)) { Keys keyData = (Keys)MyKeyboardHookStruct.VKCode; KeyEventArgs e = new KeyEventArgs(keyData); this.OnKeyUp(this, e); handled = handled || e.Handled; } } //if event handled in application do not handoff to other listeners if (handled) { return(1); } else { return(Win32API.CallNextHookEx(this.m_pKeyboardHook, nCode, wParam, lParam)); } }