Adblock breaks this site

moveMouseSmooth()

Discussion in 'Programming General' started by Blupig, Jun 3, 2010.

  1. Blupig

    Blupig BEEF TOILET
    $5 USD Donor

    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant
    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.
     
  2. super_

    super_ Member

    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0
    moveMouseSmooth()

    not even going to use a spline? only basic linear interpolation?
     
< searching youtube subscriber pusher | Alarm Clock I made in VB >


 
 
Adblock breaks this site