/// <summary> /// A callback function which will be called every Time a mouse activity detected. /// </summary> /// <param name="nCode"> /// [in] Specifies whether the hook procedure must process the message. /// If nCode is HC_ACTION, the hook procedure must process the message. /// If nCode is less than zero, the hook procedure must pass the message to the /// CallNextHookEx function without further processing and must return the /// value returned by CallNextHookEx. /// </param> /// <param name="wParam"> /// [in] Specifies whether the message was sent by the current thread. /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero. /// </param> /// <param name="lParam"> /// [in] Pointer to a CWPSTRUCT structure that contains details about the message. /// </param> /// <returns> /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook /// procedure does not call CallNextHookEx, the return value should be zero. /// </returns> private static int MouseHookProc(int nCode, int wParam, IntPtr lParam) // wparam lparam ncode받아서 대리자 객체에 넘겨줌 { if (nCode >= 0) { //Marshall the data from callback. Structs.MouseLLHookStruct mouseHookStruct = (Structs.MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(Structs.MouseLLHookStruct)); // 관련된 마우스 이벤트를 후킹해서 가져옴 //detect button clicked MouseButtons button = MouseButtons.None; short mouseDelta = 0; int clickCount = 0; bool mouseDown = false; bool mouseUp = false; switch (wParam) { case Constants.WM_LBUTTONDOWN: mouseDown = true; button = MouseButtons.Left; clickCount = 1; break; case Constants.WM_LBUTTONUP: mouseUp = true; button = MouseButtons.Left; clickCount = 1; break; case Constants.WM_LBUTTONDBLCLK: button = MouseButtons.Left; clickCount = 2; break; case Constants.WM_RBUTTONDOWN: mouseDown = true; button = MouseButtons.Right; clickCount = 1; break; case Constants.WM_RBUTTONUP: mouseUp = true; button = MouseButtons.Right; clickCount = 1; break; case Constants.WM_RBUTTONDBLCLK: button = MouseButtons.Right; clickCount = 2; break; case Constants.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; } //generate event MouseEventExtArgs e = new MouseEventExtArgs( button, clickCount, mouseHookStruct.Point.X, mouseHookStruct.Point.Y, mouseDelta); //Mouse up if (s_MouseUp != null && mouseUp) { s_MouseUp.Invoke(null, e); // 아무것도 들어갈 필요가 없어서 null 호출만 시키면 되기 때문에 if (MouseUp_Block) { return(s_MouseHookHandle); } } //Mouse down if (s_MouseDown != null && mouseDown) // 마우스가 클릭된 상황에 MouseEventHandler의 대리자 객체로 선언한 s_MouseDown(마우스클릭성공)의 이벤트를 발생시키기 위해서 s_MouseDown.Invoke(null,e)를 선언 { s_MouseDown.Invoke(null, e); if (MouseDown_Block) { return(s_MouseHookHandle); } } //If someone listens to click and a click is heppened if (s_MouseClick != null && clickCount > 0) { s_MouseClick.Invoke(null, e); } //If someone listens to click and a click is heppened if (s_MouseClickExt != null && clickCount > 0) { s_MouseClickExt.Invoke(null, e); } //If someone listens to double click and a click is heppened if (s_MouseDoubleClick != null && clickCount == 2) { s_MouseDoubleClick.Invoke(null, e); } //Wheel was moved if (s_MouseWheel != null && mouseDelta != 0) { s_MouseWheel.Invoke(null, e); } //만약 누군가가 마우스를 이동시켜 좌표가 변경되게 되면 move이벤트를 발생시킨다. if ((s_MouseMove != null || s_MouseMoveExt != 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 (s_MouseMoveExt != null) { s_MouseMoveExt.Invoke(null, e); } } if (e.Handled) { return(-1); } } //call next hook return(ImportFunctions.CallNextHookEx(s_MouseHookHandle, nCode, wParam, lParam)); }
/// 키보드 입력이 일어났을 때 훅 프로시저 호출 public int hookProc(int code, int wParam, ref keyboardHookStruct lParam) { try { if (code >= 0) { Keys key = (Keys)lParam.vkCode; if (key == Keys.None) { return(0); } if (HookedKeys.Contains(key)) { KeyEventArgs kea = new KeyEventArgs(key); if ((wParam == Constants.WM_KEYDOWN) && (KeyDown != null)) { KeyboardHooking.Instance.KeyDown(this, kea); } else if ((wParam == Constants.WM_KEYUP || wParam == Constants.WM_SYSKEYUP) && (KeyUp != null)) { if (KeyboardHooking.Instance.recording_starting && KeyboardHooking.Instance.is_startsetting) { KeyboardHooking.Instance.KeyUp(this, kea); } //KeyUp(this, kea); } else if (wParam == Constants.WM_SYSKEYDOWN) { #region 키보드로 컨트롤 하기 위한 제어문 if ((lParam.flags == 0x20) && (lParam.vkCode == (int)KeyboardHooking.Instance.RecordStartKey)) // 0x20은 alt { IsAlt = true; KeyboardHooking.Instance.LControlKeyUp(); KeyboardHooking.Instance.RecordStartButtonEvent(); return(1); } else if ((lParam.flags == 0x20) && (lParam.vkCode == (int)KeyboardHooking.Instance.LogPlayKey)) // 0x20은 alt { IsAlt = true; KeyboardHooking.Instance.LControlKeyUp(); KeyboardHooking.Instance.RecordPlayButtonEvent(); return(1); } else if ((lParam.flags == 0x20) && (lParam.vkCode == (int)KeyboardHooking.Instance.LogPlayStopKey)) // 0x20은 alt { IsAlt = true; KeyboardHooking.Instance.LControlKeyUp(); KeyboardHooking.Instance.RecordPlayStopButtonEvent(); return(1); } else if ((lParam.flags == 0x20) && (lParam.vkCode == (int)KeyboardHooking.Instance.RecordStopKey)) // 0x20은 alt { IsAlt = true; KeyboardHooking.Instance.LControlKeyUp(); KeyboardHooking.Instance.RecordStopButtonEvent(); return(1); } #endregion else if (KeyboardHooking.Instance.recording_starting && KeyboardHooking.Instance.is_startsetting) { KeyboardHooking.Instance.KeyDown(this, kea); } } else if (wParam == Constants.WM_SYSKEYUP) { } if (kea.Handled) { return(1); } } } return(ImportFunctions.CallNextHookEx(hhook, code, wParam, ref lParam)); } catch { return(0); } }