I have all the necessary code to move and click the mouse via C# code, but I don't want to just set the mouse position to X
and Y
; that will look jerky. Instead, I want to have a smooth transition from point X1, Y1
to point X2, Y2
over Z seconds. Similar to keyframing.
I'm looking for a method similar to this:
public void TransitionMouse(int x, int y, double durationInSecs)
It will just smoothly move the mouse from its current position to x
and y
in durationInSecs
seconds. I have a function called:
public void MoveMouse(int x, int y)
That moves the mouse to x
, y
immediately.
EDIT
Thanks for the help guys! Here's the finished, and tested, 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));
}
}