C# moving the mouse around realistically

后端 未结 5 1278
孤街浪徒
孤街浪徒 2020-12-02 21:44

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 mouse movements but a

5条回答
  •  甜味超标
    2020-12-02 22:23

    You can try this.

    public static class Mouse
    {
        private const float MOUSE_SMOOTH = 200f;
    
        public static void MoveTo(int targetX, int targetY)
        {
            var targetPosition = new Point(targetX, targetY);
            var curPos = Cursor.Position;
    
            var diffX = targetPosition.X - curPos.X;
            var diffY = targetPosition.Y - curPos.Y;
    
            for (int i = 0; i <= MOUSE_SMOOTH; i++)
            {
                float x = curPos.X + (diffX / MOUSE_SMOOTH * i);
                float y = curPos.Y + (diffY / MOUSE_SMOOTH * i);
                Cursor.Position = new Point((int)x, (int)y);
                Thread.Sleep(1);
            }
    
            if (Cursor.Position != targetPosition)
            {
                MoveTo(targetPosition.X, targetPosition.Y);
            }
        }
    }
    

提交回复
热议问题