• Log in or sign up for an account here: https://www.sythe.org/login

moveMouseSmooth()

Blupig

BEEF TOILET
Joined
Nov 23, 2006
Sythe Gold
1,609
Messages
7,145
Age
33
Location
Canada
moveMouseSmooth()
Fully commented etc, please credit if you use it. You'll have to make your own error handing.

Code:
public static void moveMouseSmooth(int sx, int sy, int steps){
		
		//Try the following code
		try{
		
			//Create a new robot object called mouseBot
			Robot mouseBot = new Robot();
			
			//Get current X and Y coordinates of the mouse
			//X
			int currX = MouseInfo.getPointerInfo().getLocation().x;
			//Y
			int currY = MouseInfo.getPointerInfo().getLocation().y;
			
			//Get the difference between the desired coordinates and 
			//			the current coordinates, then make sure the result is positive
			//X
			int diffX = Math.abs(sx - currX);
			//Y
			int diffY = Math.abs(sy - currY);
			
			//Calculate the interval at which to add to the coordinates as
			//			the code cycles through, then round off the division
			//X interval
			int intervalX = Math.round(diffX/steps);
			//Y interval
			int intervalY = Math.round(diffY/steps);
			
			
			//As long as the number of steps is bigger than or equal to i, do the code
			//			and add 1 onto i each time (counter)
			for(int i = 0; i <= steps; i++){
				
				//Make the application's current thread sleep for 10 ms
				//This is so that the mouse doesn't move super fast, but doesn't lag
				Thread.sleep(10);

				//"Teleport" the mouse to the current X and Y coordinates
				mouseBot.mouseMove(currX, currY);
				//Remake the current X and Y coordinates by adding the interval
				//These new coordinates are the future destinations of the mouse cursor
				//X
				currX = currX + intervalX;
				//Y
				currY = currY + intervalY;
			
			}
			
			//To eliminate any inaccuracy, "teleport" the cursor to the final position
			mouseBot.mouseMove(sx, sy);
			//Print that it was successful
			consolePrint("[mouse.smoothXY(point, int)]  mouse moved succesfully to (" + sx + ", " + sy + ")");
			
		//Catch any object errors
		} catch (AWTException e){
			
	
			
		//If the thread sleeping was interrupted...
		} catch (InterruptedException e2){

			
		}
		
	}

Don't comment on me using sleep either, for what I needed this for that was fine.
 
moveMouseSmooth()
not even going to use a spline? only basic linear interpolation?
 
Runescape Gamblingrunescape gambling
Back
Top