/// <summary> /// Moves the mouse to the requested point /// </summary> /// <param name="to">The absolute point to move the mouse to</param> public static void MoveMouse(Point to) { ManagedWinapi.InputBlocker inputBlocker = null; if (PlayerConfig.Default.BlockUserInput) { inputBlocker = new ManagedWinapi.InputBlocker(); } Stopwatch watch = new Stopwatch(); Point from = new Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y); int dx = to.X > from.X ? 1 : -1; int dy = to.Y > from.Y ? 1 : -1; double a = 0; if (from.X == to.X) { a = double.MaxValue; } else { a = (from.Y - to.Y) / (from.X - to.X); } if (Math.Abs(a) < 1) { watch.Start(); for (int x = (int)from.X; x != (int)to.X; x += dx) { double y = from.Y + (x - from.X) * a; InjectLowMouseInput(MouseEvents.MOVE, new Point(x, y)); if (PlayerConfig.Default.AnimateMouseCursor && watch.ElapsedMilliseconds < (x - (int)from.X)) { System.Threading.Thread.Sleep(1); } } watch.Stop(); } else { watch.Start(); for (int y = (int)from.Y; y != (int)to.Y; y += dy) { double x = from.X + (y - from.Y) / a; InjectLowMouseInput(MouseEvents.MOVE, new Point(x, y)); if (PlayerConfig.Default.AnimateMouseCursor && watch.ElapsedMilliseconds < (y - (int)from.Y)) { System.Threading.Thread.Sleep(1); } } watch.Stop(); } if (PlayerConfig.Default.BlockUserInput && inputBlocker != null) { inputBlocker.Dispose(); } System.Threading.Thread.Sleep(PlayerConfig.Default.DelayAfterAction); }
/// <summary> /// Clicks the mouse /// </summary> /// <param name="button">The button to click</param> /// <param name="point">The absolute point (desktop top left corener = 0,0) to click on</param> /// <param name="delayAfter">Should the action wait for 'delay after action' configuration </param> public static void Click(MouseButtons button, Point point, bool delayAfter) { ManagedWinapi.InputBlocker inputBlocker = null; if (PlayerConfig.Default.BlockUserInput) { inputBlocker = new ManagedWinapi.InputBlocker(); } if (PlayerConfig.Default.AnimateMouseCursor && (point.X != 0 || point.Y != 0)) { MoveMouse(point); } else { InjectLowMouseInput(MouseEvents.MOVE, point); } switch (button) { case MouseButtons.Left: InjectLowMouseInput(MouseEvents.LEFTDOWN, point); System.Threading.Thread.Sleep(50); InjectLowMouseInput(MouseEvents.LEFTUP, point); break; case MouseButtons.Middle: InjectLowMouseInput(MouseEvents.MIDDLEDOWN, point); System.Threading.Thread.Sleep(50); InjectLowMouseInput(MouseEvents.MIDDLEUP, point); break; case MouseButtons.Right: InjectLowMouseInput(MouseEvents.RIGHTDOWN, point); System.Threading.Thread.Sleep(50); InjectLowMouseInput(MouseEvents.RIGHTUP, point); break; case MouseButtons.XButton1: case MouseButtons.XButton2: InjectLowMouseInput(MouseEvents.XDOWN, point); System.Threading.Thread.Sleep(50); InjectLowMouseInput(MouseEvents.XUP, point); break; default: break; } if (PlayerConfig.Default.BlockUserInput && inputBlocker != null) { inputBlocker.Dispose(); } if (delayAfter) { System.Threading.Thread.Sleep(PlayerConfig.Default.DelayAfterAction); } }
/// <summary> /// Sends key strokes to the keyboard /// </summary> /// <param name="str">A unicode string of keys</param> public static void SendKeystrokes(string str) { ManagedWinapi.InputBlocker inputBlocker = null; if (PlayerConfig.Default.BlockUserInput) { inputBlocker = new ManagedWinapi.InputBlocker(); } bool shouldPressDown = false; List <char> pressedDownChars = new List <char>(); List <Key> pressedDownKeys = new List <Key>(); for (int i = 0; i < str.Length; i++) { char c = str[i]; switch (c) { case '{': Key key = ParseKeyInBraces(str, i + 1, out i); if (key != Key.None) { if (shouldPressDown) { pressedDownKeys.Add(key); PressExtendedKey(key); } else { TapExtendedKey(key); } } break; case '(': shouldPressDown = true; break; case ')': shouldPressDown = false; foreach (char pressedChar in pressedDownChars) { ReleaseKey(pressedChar); } foreach (Key pressedKey in pressedDownKeys) { ReleaseExtendedKey(pressedKey); } pressedDownChars.Clear(); pressedDownKeys.Clear(); break; default: if (shouldPressDown) { pressedDownChars.Add(c); PressAnsiKey(c); } else { TapKey(c); } break; } } foreach (char pressedChar in pressedDownChars) { ReleaseAnsiKey(pressedChar); } foreach (Key pressedKey in pressedDownKeys) { ReleaseExtendedKey(pressedKey); } if (PlayerConfig.Default.BlockUserInput) { inputBlocker.Dispose(); } }