private static int KeyboardHookProc(int nCode, int wParam, IntPtr lParam) { var handled = false; if (nCode >= 0) { var struct2 = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct)); if (Instance.SKeyDown != null && (wParam == 0x100 || wParam == 260)) { var e = new KeyEventArgs((Keys)struct2.VirtualKeyCode); Instance.SKeyDown(null, e); handled = e.Handled; } if (Instance.SKeyPress != null && wParam == 0x100) { var flag2 = (Win32Interop.GetKeyState(0x10) & 0x80) == 0x80; var flag3 = Win32Interop.GetKeyState(20) != 0; var pbKeyState = new byte[0x100]; Win32Interop.GetKeyboardState(pbKeyState); var lpwTransKey = new byte[2]; if (Win32Interop.ToAscii(struct2.VirtualKeyCode, struct2.ScanCode, pbKeyState, lpwTransKey, struct2.Flags) == 1) { var c = (char)lpwTransKey[0]; if (flag3 ^ flag2 && char.IsLetter(c)) { c = char.ToUpper(c); } var args2 = new KeyPressEventArgs(c); Instance.SKeyPress(null, args2); handled = handled || args2.Handled; } } if (Instance.SKeyUp != null && (wParam == 0x101 || wParam == 0x105)) { var args3 = new KeyEventArgs((Keys)struct2.VirtualKeyCode); Instance.SKeyUp(null, args3); handled = handled || args3.Handled; } } if (handled) { return(-1); } return(Win32Interop.CallNextHookEx(Instance._sKeyboardHookHandle, nCode, wParam, lParam)); }
private static int MouseHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { var struct2 = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct)); var none = MouseButtons.None; short delta = 0; var clicks = 0; var flag = false; var flag2 = false; switch (wParam) { case 0x201: flag = true; none = MouseButtons.Left; clicks = 1; break; case 0x202: flag2 = true; none = MouseButtons.Left; clicks = 1; break; case 0x203: none = MouseButtons.Left; clicks = 2; break; case 0x204: flag = true; none = MouseButtons.Right; clicks = 1; break; case 0x205: flag2 = true; none = MouseButtons.Right; clicks = 1; break; case 0x206: none = MouseButtons.Right; clicks = 2; break; case 0x20a: try { delta = (short)((struct2.MouseData >> 0x10) & 0xffff); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } break; } var e = new MouseEventExtArgs(none, clicks, struct2.Point.X, struct2.Point.Y, delta); if (Instance.SMouseUp != null && flag2) { Instance.SMouseUp(null, e); } if (Instance.SMouseDown != null && flag) { Instance.SMouseDown(null, e); } if (Instance.SMouseClick != null && clicks > 0) { Instance.SMouseClick(null, e); } if (Instance.SMouseClickExt != null && clicks > 0) { Instance.SMouseClickExt(null, e); } if (Instance.SMouseDoubleClick != null && clicks == 2) { Instance.SMouseDoubleClick(null, e); } if (Instance.SMouseWheel != null && delta != 0) { Instance.SMouseWheel(null, e); } if ((Instance.SMouseMove != null || Instance.SMouseMoveExt != null) && (Instance._oldX != struct2.Point.X || Instance._oldY != struct2.Point.Y)) { Instance._oldX = struct2.Point.X; Instance._oldY = struct2.Point.Y; if (Instance.SMouseMove != null) { Instance.SMouseMove(null, e); } if (Instance.SMouseMoveExt != null) { Instance.SMouseMoveExt(null, e); } } if (e.Handled) { return(-1); } } return(Win32Interop.CallNextHookEx(Instance._sMouseHookHandle, nCode, wParam, lParam)); }