void SendMouseInput(Point point, User32.MOUSEEVENTF flags, long timestamp) { User32.INPUT[] input = new User32.INPUT[1]; input[0].type = User32.SendInputType.Mouse; input[0].U.mi.dx = (int)(point.X * 65535.0); input[0].U.mi.dy = (int)(point.Y * 65535.0); input[0].U.mi.dwFlags = flags; input[0].U.mi.time = 0; //(uint)timestamp / 1000; User32.SendInput(1, input, Marshal.SizeOf(typeof(User32.INPUT))); }
/*private void SimulateGestureBtnEvent(GestureBtnEventType eventType, int x, int y) * { * const int CLICK_PRESS_RELEASE_INTERVAL = 10; * * Debug.WriteLine("SimulateMouseEvent: " + _gestureBtn + " " + eventType); * _simulatingMouse = true; * * User32.SetCursorPos(x, y); * * var mouseSwapped = Native.IsMouseButtonSwapped(); * * switch (_gestureBtn) * { * case GestureTriggerButton.Right: * if (mouseSwapped) * { * switch (eventType) * { * case GestureBtnEventType.UP: * User32.mouse_event(User32.MOUSEEVENTF.MOUSEEVENTF_LEFTUP, x, y, 0, MOUSE_EVENT_EXTRA_SIMULATED); * break; * case GestureBtnEventType.DOWN: * User32.mouse_event(User32.MOUSEEVENTF.MOUSEEVENTF_LEFTDOWN, x, y, 0, MOUSE_EVENT_EXTRA_SIMULATED); * break; * case GestureBtnEventType.CLICK: * User32.mouse_event(User32.MOUSEEVENTF.MOUSEEVENTF_LEFTDOWN, x, y, 0, MOUSE_EVENT_EXTRA_SIMULATED); * Thread.Sleep(CLICK_PRESS_RELEASE_INTERVAL); * User32.mouse_event(User32.MOUSEEVENTF.MOUSEEVENTF_LEFTUP, x, y, 0, MOUSE_EVENT_EXTRA_SIMULATED); * break; * } * } * else * { * switch (eventType) * { * case GestureBtnEventType.UP: * User32.mouse_event(User32.MOUSEEVENTF.MOUSEEVENTF_RIGHTUP, x, y, 0, MOUSE_EVENT_EXTRA_SIMULATED); * break; * case GestureBtnEventType.DOWN: * User32.mouse_event(User32.MOUSEEVENTF.MOUSEEVENTF_RIGHTDOWN, x, y, 0, MOUSE_EVENT_EXTRA_SIMULATED); * break; * case GestureBtnEventType.CLICK: * User32.mouse_event(User32.MOUSEEVENTF.MOUSEEVENTF_RIGHTDOWN, x, y, 0, MOUSE_EVENT_EXTRA_SIMULATED); * Thread.Sleep(CLICK_PRESS_RELEASE_INTERVAL); * User32.mouse_event(User32.MOUSEEVENTF.MOUSEEVENTF_RIGHTUP, x, y, 0, MOUSE_EVENT_EXTRA_SIMULATED); * break; * } * } * break; * case GestureTriggerButton.Middle: * switch (eventType) * { * case GestureBtnEventType.UP: * User32.mouse_event(User32.MOUSEEVENTF.MOUSEEVENTF_MIDDLEUP, x, y, 0, MOUSE_EVENT_EXTRA_SIMULATED); * * break; * case GestureBtnEventType.DOWN: * User32.mouse_event(User32.MOUSEEVENTF.MOUSEEVENTF_MIDDLEDOWN, x, y, 0, MOUSE_EVENT_EXTRA_SIMULATED); * * break; * case GestureBtnEventType.CLICK: * User32.mouse_event(User32.MOUSEEVENTF.MOUSEEVENTF_MIDDLEDOWN, x, y, 0, MOUSE_EVENT_EXTRA_SIMULATED); * Thread.Sleep(CLICK_PRESS_RELEASE_INTERVAL); * User32.mouse_event(User32.MOUSEEVENTF.MOUSEEVENTF_MIDDLEUP, x, y, 0, MOUSE_EVENT_EXTRA_SIMULATED); * break; * } * break; * * case GestureTriggerButton.X1: * case GestureTriggerButton.X2: * DoMouseEvent(eventType, User32.MOUSEEVENTF.MOUSEEVENTF_XUP) * break; * } * _simulatingMouse = false; * }*/ private void SimulateMouseEvent(User32.MOUSEEVENTF e, int x, int y, uint data = 0) { Debug.WriteLine("SimulateMouseEvent: " + e); _simulatingInput = true; User32.SetCursorPos(x, y); User32.mouse_event(e, x, y, data, SIMULATED_EVENT_TAG); _simulatingInput = false; }
private unsafe static void SendMouseInput(int x, int y, User32.MOUSEEVENTF flags) { if ((flags & User32.MOUSEEVENTF.ABSOLUTE) != 0) { int vscreenWidth = User32.GetSystemMetrics(User32.SystemMetric.SM_CXVIRTUALSCREEN); int vscreenHeight = User32.GetSystemMetrics(User32.SystemMetric.SM_CYVIRTUALSCREEN); int vscreenLeft = User32.GetSystemMetrics(User32.SystemMetric.SM_XVIRTUALSCREEN); int vscreenTop = User32.GetSystemMetrics(User32.SystemMetric.SM_YVIRTUALSCREEN); const int DesktopNormilizedMax = 65536; // Absolute input requires that input is in 'normalized' coords - with the entire // desktop being (0,0)...(65535,65536). Need to convert input x,y coords to this // first. // // In this normalized world, any pixel on the screen corresponds to a block of values // of normalized coords - eg. on a 1024x768 screen, // y pixel 0 corresponds to range 0 to 85.333, // y pixel 1 corresponds to range 85.333 to 170.666, // y pixel 2 correpsonds to range 170.666 to 256 - and so on. // Doing basic scaling math - (x-top)*65536/Width - gets us the start of the range. // However, because int math is used, this can end up being rounded into the wrong // pixel. For example, if we wanted pixel 1, we'd get 85.333, but that comes out as // 85 as an int, which falls into pixel 0's range - and that's where the pointer goes. // To avoid this, we add on half-a-"screen pixel"'s worth of normalized coords - to // push us into the middle of any given pixel's range - that's the 65536/(Width*2) // part of the formula. So now pixel 1 maps to 85+42 = 127 - which is comfortably // in the middle of that pixel's block. // The key ting here is that unlike points in coordinate geometry, pixels take up // space, so are often better treated like rectangles - and if you want to target // a particular pixel, target its rectangle's midpoint, not its edge. x = ((x - vscreenLeft) * DesktopNormilizedMax) / vscreenWidth + DesktopNormilizedMax / (vscreenWidth * 2); y = ((y - vscreenTop) * DesktopNormilizedMax) / vscreenHeight + DesktopNormilizedMax / (vscreenHeight * 2); flags |= User32.MOUSEEVENTF.VIRTUALDESK; } var mouseInput = new User32.INPUT(); mouseInput.type = User32.INPUTENUM.MOUSE; mouseInput.inputUnion.mi.dx = x; mouseInput.inputUnion.mi.dy = y; mouseInput.inputUnion.mi.mouseData = 0; mouseInput.inputUnion.mi.dwFlags = flags; mouseInput.inputUnion.mi.time = 0; mouseInput.inputUnion.mi.dwExtraInfo = IntPtr.Zero; User32.SendInput(1, &mouseInput, Marshal.SizeOf(mouseInput)); }
private void SimulateGestureBtnEvent(GestureBtnEventType eventType, int x, int y) { Debug.WriteLine("SimulateMouseEvent: " + _gestureBtn + " " + eventType); _simulatingMouse = true; var mouseSwapped = Native.GetSystemMetrics(Native.SystemMetric.SM_SWAPBUTTON) != 0; Native.POINT curPos; Native.GetCursorPos(out curPos); User32.MOUSEEVENTF events = 0; switch (_gestureBtn) { case GestureButtons.RightButton: if (mouseSwapped) { switch (eventType) { case GestureBtnEventType.UP: events = User32.MOUSEEVENTF.MOUSEEVENTF_LEFTUP; break; case GestureBtnEventType.DOWN: events = User32.MOUSEEVENTF.MOUSEEVENTF_LEFTDOWN; break; case GestureBtnEventType.CLICK: events = User32.MOUSEEVENTF.MOUSEEVENTF_LEFTDOWN | User32.MOUSEEVENTF.MOUSEEVENTF_LEFTUP; break; } } else { switch (eventType) { case GestureBtnEventType.UP: events = User32.MOUSEEVENTF.MOUSEEVENTF_RIGHTUP; break; case GestureBtnEventType.DOWN: events = User32.MOUSEEVENTF.MOUSEEVENTF_RIGHTDOWN; break; case GestureBtnEventType.CLICK: events = User32.MOUSEEVENTF.MOUSEEVENTF_RIGHTDOWN | User32.MOUSEEVENTF.MOUSEEVENTF_RIGHTUP; break; } } break; case GestureButtons.MiddleButton: switch (eventType) { case GestureBtnEventType.UP: events = User32.MOUSEEVENTF.MOUSEEVENTF_MIDDLEUP; break; case GestureBtnEventType.DOWN: events = User32.MOUSEEVENTF.MOUSEEVENTF_MIDDLEDOWN; break; case GestureBtnEventType.CLICK: events = User32.MOUSEEVENTF.MOUSEEVENTF_MIDDLEDOWN | User32.MOUSEEVENTF.MOUSEEVENTF_MIDDLEUP; break; } break; } User32.SetCursorPos(x, y); //User32.SetMessageExtraInfo(new IntPtr(MOUSE_EVENT_EXTRA_SIMULATED)); User32.mouse_event(events, x, y, 0, MOUSE_EVENT_EXTRA_SIMULATED); //var sim = new InputSimulator(); //sim.Mouse.RightButtonDown(); //sim.Mouse.Sleep(10); //sim.Mouse.RightButtonUp(); _simulatingMouse = false; }