2

I'm struggling to figure out why the below function is only ever moving my mouse from 0, 0 screen coords, to my final destination, even though Cursor.Position is returning the correct screen coords. If anybody could enlighten me i'd be most grateful.

public void MoveAndClick(int x, int y, int steps) { Point start = Cursor.Position; PointF iterPoint = start; PointF slope = new PointF(x - start.X, y - start.Y); slope.X = slope.X / steps; slope.Y = slope.Y / steps; for(int i=0; i<steps; i++) { iterPoint = new PointF(iterPoint.X + slope.X, iterPoint.Y + slope.Y); inputSim.Mouse.MoveMouseTo(iterPoint.X, iterPoint.Y); Thread.Sleep(10); } inputSim.Mouse.MoveMouseTo(x, y); inputSim.Mouse.RightButtonDown(); inputSim.Mouse.RightButtonUp(); } 

Taken from https://stackoverflow.com/a/913703/2014393

3 Answers 3

4

DOH! Always stop working when you're sleepy and go to bed.

The InputSimulator library requires that you translate coordinates like so:

int startX = 65535 * Cursor.Position.X / Screen.PrimaryScreen.Bounds.Width; int startY = 65535 * Cursor.Position.Y / Screen.PrimaryScreen.Bounds.Height; 

I was converting the end coordinates but completely forgot to translate the starting coordinates, derp.

Sign up to request clarification or add additional context in comments.

Comments

0

FlaUI allows for the mouse to move smoothly. Here is the Mouse class in FlaUI for reference.

Comments

0
public static void MoverSuavementePara(string arg) { (int, int) pos = RegexHelper.pos_math(arg); int endX = (int)(pos.Item1 * Screen.PrimaryScreen.Bounds.Width); int endY = (int)(pos.Item2 * Screen.PrimaryScreen.Bounds.Height); (int, int) cord_start = NormalizarCoordenadas(Cursor.Position.X, Cursor.Position.Y); int startX = cord_start.Item1; int startY = cord_start.Item2; int duracao = 890; DateTime startTime = DateTime.Now; while ((DateTime.Now - startTime).TotalMilliseconds <= duracao) { double progress = (DateTime.Now - startTime).TotalMilliseconds / duracao; int currentX = (int)(startX + progress * (endX - startX)); int currentY = (int)(startY + progress * (endY - startY)); Inputer.inputer.Mouse.MoveMouseTo(currentX, currentY); System.Threading.Thread.Sleep(10); } Inputer.inputer.Mouse.MoveMouseTo(endX, endY); } static (int, int) NormalizarCoordenadas(int x, int y) { return ( NormalizarCoordenada(x, SystemInformation.VirtualScreen.Left, SystemInformation.VirtualScreen.Right, 65535), NormalizarCoordenada(y, SystemInformation.VirtualScreen.Top, SystemInformation.VirtualScreen.Bottom, 65535) ); } static int NormalizarCoordenada(int coordenada, int telaMin, int telaMax, int alvoMax) { return (int)((double)(coordenada - telaMin) / (telaMax - telaMin) * alvoMax); } 

1 Comment

Hi Rafael! Please edit your answer to translate the non-English variables, and to remove unrelated parts (instead of telling readers to ignore them), that would be help to future readers immensely. I also suggest taking the tour and reading How to Answer to better understand how to contribute to Stack Overflow, as this is not a forum, but a high quality Q&A website. Thanks for contributing!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.