I have all the necessary code to move and click with the C # code, but I do not want to just set the mouse position in X and Y ; it will look jerky. Instead, I want to have a smooth transition from point X1, Y1 to point X2, Y2 in Z seconds. Like keyframing.
I am looking for a method similar to this:
public void TransitionMouse(int x, int y, double durationInSecs)
It just smoothly moves the mouse from its current position to X and Y in durationInSecs seconds. I have a function:
public void MoveMouse(int x, int y)
This instantly moves the mouse in X , Y
EDIT
Thanks for the help guys! Here is the finished and verified code:
[DllImport("user32.dll")] static extern bool SetCursorPos(int X, int Y); public void TransitionMouseTo(double x, double y, double durationSecs) { double frames = durationSecs*100; PointF vector = new PointF(); PointF mousePos = Cursor.Position; vector.X = (float)((x - mousePos.X) / frames); vector.Y = (float)((y - mousePos.Y) / frames); for (int i = 0; i < frames; i++) { SetCursorPos((int)(mousePos.X += vector.X), (int)(mousePos.Y += vector.Y)); Thread.Sleep((int)((durationSecs / frames) * 1000.0)); } }
source share