/// <summary> /// /// </summary> /// <param name="code"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> public Int32 MyMouseCallBack(Int32 code, WindowsMessages wParam, MSLLHOOKSTRUCT lParam) { if (code < 0) { return(NativeMethod.CallNextHookEx(IntPtr.Zero, code, wParam, lParam)); } switch (wParam) { case WindowsMessages.WM_MOUSEMOVE: if (MouseMove == null) { break; } MouseMove(lParam.pt.X, lParam.pt.Y); break; case WindowsMessages.WM_LBUTTONDOWN: if (MouseLeftDown == null) { break; } MouseLeftDown(lParam.pt.X, lParam.pt.Y); break; } // キーの処理を次に回して完了。 return(NativeMethod.CallNextHookEx(IntPtr.Zero, code, wParam, lParam)); }
/// <summary> /// Low-level keyboard hook procedure. /// The system calls this function every time a new keyboard input event /// is about to be posted into a thread input queue. The keyboard input /// can come from the local keyboard driver or from calls to the /// keybd_event function. If the input comes from a call to keybd_event, /// the input was "injected". However, the WH_KEYBOARD_LL hook is not /// injected into another process. Instead, the context switches back /// to the process that installed the hook and it is called in its /// original context. Then the context switches back to the application /// that generated the event. /// </summary> /// <param name="nCode"> /// The hook code passed to the current hook procedure. /// When nCode equals HC_ACTION, it means that the wParam and lParam /// parameters contain information about a keyboard message. /// </param> /// <param name="wParam"> /// The parameter can be one of the following messages: /// WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP. /// </param> /// <param name="lParam">Pointer to a KBDLLHOOKSTRUCT structure.</param> /// <returns></returns> /// <see cref="http://msdn.microsoft.com/en-us/library/ms644985.aspx"/> public int LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { // Marshal the KeyboardHookStruct data from the callback lParam KBDLLHOOKSTRUCT keyboardLLHookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT)); // Get the virtual key code from KBDLLHOOKSTRUCT.vkCode // http://msdn.microsoft.com/en-us/library/dd375731.aspx Key vkCode = (Key)keyboardLLHookStruct.vkCode; // Get the keyboard WM from the wParam parameter KeyboardMessage wmKeyboard = (KeyboardMessage)wParam; if (wmKeyboard == KeyboardMessage.WM_KEYUP) { KeyUpEvent keyUp = OnKeyUp; if (keyUp != null) { Task.Factory.StartNew(delegate() { keyUp(); }); } } } // Pass the hook information to the next hook procedure in chain return(NativeMethod.CallNextHookEx(hGlobalLLKeyboardHook, nCode, wParam, lParam)); }
/// <summary> /// Low-level mouse hook procedure /// The system call this function every time a new mouse input event is /// about to be posted into a thread input queue. The mouse input can come /// from the local mouse driver or from calls to the mouse_event function. /// If the input comes from a call to mouse_event, the input was /// "injected". However, the WH_MOUSE_LL hook is not injected into another /// process. Instead, the context switches back to the process that /// installed the hook and it is called in its original context. Then the /// context switches back to the application that generated the event. /// </summary> /// <param name="nCode"> /// The hook code passed to the current hook procedure. /// When nCode equals HC_ACTION, the wParam and lParam parameters contain /// information about a mouse message. /// </param> /// <param name="wParam"> /// This parameter can be one of the following messages: /// WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MOUSEMOVE, WM_MOUSEWHEEL, /// WM_MOUSEHWHEEL, WM_RBUTTONDOWN, or WM_RBUTTONUP. /// </param> /// <param name="lParam">Pointer to an MSLLHOOKSTRUCT structure.</param> /// <returns></returns> /// <see cref="http://msdn.microsoft.com/en-us/library/ms644986.aspx"/> public int LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { // Marshal the MSLLHOOKSTRUCT data from the callback lParam MSLLHOOKSTRUCT mouseLLHookStruct = (MSLLHOOKSTRUCT) Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); // Get the mouse WM from the wParam parameter MouseMessage wmMouse = (MouseMessage)wParam; if (wmMouse == MouseMessage.WM_LBUTTONUP) { MouseUpEvent mouseUp = OnMouseUp; if (mouseUp != null) { Task.Factory.StartNew(delegate() { mouseUp(new Point(mouseLLHookStruct.pt.x, mouseLLHookStruct.pt.y)); }); //ThreadPool.QueueUserWorkItem((o) => mouseUp(new Point(mouseLLHookStruct.pt.x, mouseLLHookStruct.pt.y))); } } } // Pass the hook information to the next hook procedure in chain return(NativeMethod.CallNextHookEx(hGlobalLLMouseHook, nCode, wParam, lParam)); }
private int CallNextHook(int code, MouseMessage message, ref MOUSESTATE state) { if (code >= 0) { OnMouseHooked(new MouseHookedEventArgs(message, ref state)); } return(NativeMethod.CallNextHookEx(hook, code, message, ref state)); }
private int CallNextHook(int code, KeyboardMessage message, ref KEYBOARDSTATE state) { if (code >= 0) { KeyboardHookedEventArgs e = new KeyboardHookedEventArgs(message, ref state); OnKeyboardHooked(e); if (e.Cancel) { return(-1); } } return(NativeMethod.CallNextHookEx(IntPtr.Zero, code, message, ref state)); }
/// <summary> /// /// </summary> /// <param name="code"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> public Int32 MyKeyCallBack(Int32 code, WindowsMessages wParam, KBDLLHOOKSTRUCT lParam) { if (code < 0) { return(NativeMethod.CallNextHookEx(IntPtr.Zero, code, wParam, lParam)); } Int32 key; switch (wParam) { case WindowsMessages.WM_IME_KEYDOWN: case WindowsMessages.WM_KEYDOWN: case WindowsMessages.WM_SYSKEYDOWN: key = lParam.vkCode; if (KeyDown == null) { break; } KeyDown(key); break; case WindowsMessages.WM_IME_KEYUP: case WindowsMessages.WM_KEYUP: case WindowsMessages.WM_SYSKEYUP: key = lParam.vkCode; if (KeyUp == null) { break; } KeyUp(key); break; } // キーの処理を次に回して完了。 return(NativeMethod.CallNextHookEx(IntPtr.Zero, code, wParam, lParam)); }