Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default making mouse movements human like [help]

    Java Code:
    Robot robot;
    		MouseInfo i = null;
    		
    		try {
    			int preX = i.getPointerInfo().getLocation().x;
    			int preY = i.getPointerInfo().getLocation().y;
    			scanning:
    			while (preX != x && preY != y){
    				if (preX == x){
    					
    				}
    				else if (preX > x){
    					preX = preX-4;
    				}
    				else if (preX < x){
    					preX = preX+4;
    				}
    				if (preY == y){
    					
    				}
    				else if (preY > y){
    					preY = preY-4;
    				}
    				else if (preY < y){
    					preY = preY+4;
    				}
    				if (preY == y && preX == x){
    					break scanning;
    				}
    				robot = new Robot();
    				int rnd = Methods.random(1,500);
    				if (rnd == 5){
    					robot.mouseMove(preX+2, preY+3);
    					robot.delay(Methods.random(500, 600));
    					preX = preX + 1;
    					preY = preY + 1;
    				}
    			
    			robot.mouseMove(preX, preY);
    			robot.delay(Methods.random(10, 15));
    			}
    		} catch (AWTException e) {
    
    			e.printStackTrace();
    		}
    I am trying to make a mouse movement human-like, because the normal robot class is extremely un-human. When i try to run the above code, the mouse does not move to the right location, and is awfully buggy. Any tips? Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    When i try to run the above code, the mouse does not move to the right location, and is awfully buggy
    Can you describe what happens?

    What is the "right location" as compared to the location where the cursor is moved to?
    Above, below, to left, to right???

  3. #3
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Thanks Norn for your reply-
    I forgot to include that this was a method that you can call like this:
    moveMouse(int x, int y);
    So the "right location" in this case are the parameters passed on to the method. For example, i pass on the parameters to be '500,500'; The method should move the mouse to that spot. Instead, the mouse path unpredictably moves.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    the mouse path unpredictably moves
    Hard to believe.
    Does the cursor move to a new unique place every time you use 500, 500?
    Or does it move to the same place every time?

  5. #5
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Hard to believe.
    Does the cursor move to a new unique place every time you use 500, 500?
    Or does it move to the same place every time?
    It moves to some random place, that is clearly not 500,500.

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    I don't see anything that guarantees the mouse will ever stop moving and end up where you want it in any reasonable number of time. In your while loop, you are always adding or subtracting 4, except when you enter the random portion and you add 1. Given enough time (on the order of minutes it looks like), it should eventually hit the right spot as long as nothing else changes the pointer location. My guess is you are calling this method from multiple threads that are all trying to move the pointer around and interfereing with eachother.

  7. #7
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Quote Originally Posted by toadaly View Post
    I don't see anything that guarantees the mouse will ever stop moving and end up where you want it in any reasonable number of time. In your while loop, you are always adding or subtracting 4, except when you enter the random portion and you add 1. Given enough time (on the order of minutes it looks like), it should eventually hit the right spot as long as nothing else changes the pointer location. My guess is you are calling this method from multiple threads that are all trying to move the pointer around and interfereing with eachother.
    Thanks for your reply-
    This is not the case though, as this is all ran on a single thread. There is only one instance where i run it.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Does the cursor move to a new unique place every time you use 500, 500?
    Or does it move to the same place every time given the 500, 500 location?

    robot.mouseMove(preX+2, preY+3);

    What are the values of preX+2 and preY+3?

  9. #9
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Does the cursor move to a new unique place every time you use 500, 500?
    Or does it move to the same place every time given the 500, 500 location?

    robot.mouseMove(preX+2, preY+3);

    What are the values of preX+2 and preY+3?
    It moves to a unique, new place every time. And that there is the human like feature to make it look like it is not moving in a perfectly strait line every once in a while.

    The values of preX and preY are both the mouse positions (preX = current x, preY = currentY). So preX is the current mouse position + 2, and preY is +3.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    It moves to a unique, new place every time.
    That's interesting. Computers usually do the same thing with the same input data, but you say your program moves the cursor to a different place every time it is given 500,500.

    What are the values of preX+2 and preY+3?

    Can you make a small program and post it that compiles and executes that demonstrates this?

  11. #11
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Okay, it looks like it works now. Just one little problem now:
    When the mouse gets very close to the 500,500 spot, it moves around crazily because of the random function i put into there

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    it moves around crazily because of the random function i put into there
    Remove/fix your code.

  13. #13
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Very strange. Even when i remove it, it still "spazzes" out.

  14. #14
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Okay, here is the complete method source:
    Java Code:
    	public void moveTo(int x, int y) {
    		Robot robot;
    		MouseInfo i = null;
    		
    		try {
    			int preX = i.getPointerInfo().getLocation().x;
    			int preY = i.getPointerInfo().getLocation().y;
    			scanning:
    			while (preX != x && preY != y){
    				
    				 preX = i.getPointerInfo().getLocation().x;
    				 preY = i.getPointerInfo().getLocation().y;
    				
    				if (preX == x){
    					
    				}
    				
    				if (preX != x && preY == y && preX > x){
    					preX = preX - 2;
    				}
    				else if (preX != x && preY == y && preX < x){
    					preX = preX + 2;
    				}
    				else if (preY != y && preX == x && preY < x){
    					preY = preY + 2;
    				}
    				else if (preY != y && preX == x && preY > x){
    					preY = preY -2;
    				}
    				else if (preX > x){
    					preX = preX-2;
    				}
    				else if (preX < x){
    					preX = preX+2;
    				}
    				if (preY == y){
    					
    				}
    				else if (preY > y){
    					preY = preY-2;
    				}
    				else if (preY < y){
    					preY = preY+2;
    				}
    				if (preY == y && preX == x){
    					System.out.println("hit");
    					break scanning;
    				}
    				robot = new Robot();
    				
    			
    			robot.mouseMove(preX, preY);
    			robot.delay(Methods.random(10, 15));
    			}
    		} catch (AWTException e) {
    
    			e.printStackTrace();
    		}
    
    	}

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I don't understand how the code you posted can work:
    Java Code:
    MouseInfo i = null;
    		
    		try {
    			int preX = i.getPointerInfo().getLocation().x;
    Do you get a NPE because i is null?

  16. #16
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Actually, it works. It does the job that i need it to (returns to mouse position). So setting it null doesn't effect it...for as far as i know.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Actually, it works.
    I don't believe the code you posted will work without getting a NPE.
    Calling a method using a null variable can't work.

  18. #18
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    You should try it. But in the meantime, how should i fix it?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Yes it works. I didn't lookup the methods to see they were static.
    Clearer coding would prevent others from misreading your code:

    int preX = MouseInfo.getPointerInfo().getLocation().x; // use normal static method ref

  20. #20
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Oh okay. It works better now! But, there is still a major issue: It does not always go to the right place.

Page 1 of 2 12 LastLast

Similar Threads

  1. Mouse
    By PhQ in forum AWT / Swing
    Replies: 18
    Last Post: 08-18-2010, 04:05 PM
  2. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 04:34 AM
  3. Replies: 1
    Last Post: 08-11-2009, 03:10 PM
  4. Replies: 0
    Last Post: 11-06-2007, 02:20 PM
  5. mouse over on JButton
    By gradon in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 05:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •