tags:

views:

781

answers:

3

Hi,

I am demoing a piece of software and want to build a mouse 'mover' function so that I can basically automate the process. I want to create realistic move movements but am having a bit of a mental block in the thought process. I can move a mouse around easily with c# but want it to be a bit more realistic than just the cursor appearing at a certain x, y, coordinates and then pressing a button.

I get the current position of the mouse and then get the end point. Calculate an arc between the two points, but then I need to calculate points along that arc so that I can add a timer event into that so that I can move from one point to the next, and then repeat this till I get to the target...

Anybody want to elaborate?

Thanks, R.

+1  A: 

A usual way, I think, is to physically move the real mouse with your own hand: and have the software capture those (real) movements, and replay them.

ChrisW
+3  A: 

I tried the arc calculation method, turned out to be far to complex and, in the end, it didn't look realistic. Straight lines look much more human, as JP suggests in his comment.

This is a function I wrote to calculate a linear mouse movement. Should be pretty self-explanatory. GetCursorPosition() and SetCursorPosition(Point) are wrappers around the win32 functions GetCursorPos and SetCursorPos.

public void LinearSmoothMove(Point newPosition, int steps) {
 Point start = GetCursorPosition();
 PointF iterPoint = start;

 // Find the slope of the line segment defined by start and newPosition
 PointF slope = new PointF(newPosition.X - start.X, newPosition.Y - start.Y);

 // Divide by the number of steps
 slope.X = slope.X / steps;
 slope.Y = slope.Y / steps;

 // Move the mouse to each iterative point.
 for (int i = 0; i < steps; i++)
 {
  iterPoint = new PointF(iterPoint.X + slope.X, iterPoint.Y + slope.Y);
  SetCursorPosition(Point.Round(iterPoint));
  Thread.Sleep(MouseEventDelayMS);
 }

 // Move the mouse to the final destination.
 SetCursorPosition(newPosition);
}
Erik Forbes
I knew somebody would be smart with the math! :)
JP Alioto
Glad I didn't disappoint. =)
Erik Forbes
Erik, I thought I replied to this last night but obviously I was more tired than I thought. Thanks, its a great answer and definitely gives me a) something I can use, b) something I can expand on. Great answer. I'll bear in mind what you said about the arc.
flavour404
No worries. Glad to help. I'll see if I can dig up the old code I wrote that used arcs and post it if I find it.
Erik Forbes
@Erik,What's MouseEventDelayMS?
Ashley Simpson
Sorry - an int constant that determines the number of milliseconds to wait in between move events.
Erik Forbes
A: 
Thanks someone, i'll take a look. Interesting moniker! :)
flavour404
I have been trying to work out what the minE() function does, any idea?
flavour404