Results 1 to 20 of 23
- 08-19-2010, 03:44 AM #1
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
making mouse movements human like [help]
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!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(); }
- 08-19-2010, 02:01 PM #2
Can you describe what happens?When i try to run the above code, the mouse does not move to the right location, and is awfully buggy
What is the "right location" as compared to the location where the cursor is moved to?
Above, below, to left, to right???
- 08-19-2010, 03:43 PM #3
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
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.
- 08-19-2010, 03:52 PM #4
Hard to believe.the mouse path unpredictably moves
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?
- 08-19-2010, 04:24 PM #5
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
- 08-19-2010, 04:25 PM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 08-19-2010, 04:40 PM #7
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
- 08-19-2010, 05:14 PM #8
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?
- 08-19-2010, 05:24 PM #9
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
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.
- 08-19-2010, 05:27 PM #10
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.It moves to a unique, new place every time.
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?
- 08-19-2010, 06:02 PM #11
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
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
- 08-19-2010, 06:07 PM #12
Remove/fix your code.it moves around crazily because of the random function i put into there
- 08-19-2010, 06:43 PM #13
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
Very strange. Even when i remove it, it still "spazzes" out.
- 08-20-2010, 06:28 PM #14
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
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(); } }
- 08-20-2010, 07:44 PM #15
I don't understand how the code you posted can work:
Do you get a NPE because i is null?Java Code:MouseInfo i = null; try { int preX = i.getPointerInfo().getLocation().x;
- 08-20-2010, 07:48 PM #16
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
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.
- 08-20-2010, 08:52 PM #17
I don't believe the code you posted will work without getting a NPE.Actually, it works.
Calling a method using a null variable can't work.
- 08-20-2010, 09:02 PM #18
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
You should try it. But in the meantime, how should i fix it?
- 08-20-2010, 09:15 PM #19
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
- 08-20-2010, 09:22 PM #20
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
Similar Threads
-
Mouse
By PhQ in forum AWT / SwingReplies: 18Last Post: 08-18-2010, 04:05 PM -
Mouse Listener for mouse floating over object?
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-18-2009, 04:34 AM -
Manpower Recruitment Employment Staffing Human Resources Pakistan
By junaid991 in forum Jobs OfferedReplies: 1Last Post: 08-11-2009, 03:10 PM -
Is there any utility that reads TimeZone data and display it in Human readable format
By Santhosh in forum Advanced JavaReplies: 0Last Post: 11-06-2007, 02:20 PM -
mouse over on JButton
By gradon in forum Java AppletsReplies: 1Last Post: 08-04-2007, 05:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks