How to move a mouse by using java code?

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 10611 Views - Last Post: 24 February 2012 - 09:00 AM Rate Topic: -----

#1 dlee32695  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-February 12

How to move a mouse by using java code?

Posted 21 February 2012 - 05:30 PM

There is mouseMove()method that makes the pointer jump to that location. I want to be able to make the mouse move smoothly throughout the screen. I need to write a method named mouseGLide() which takes a start x, start y, end x, end y, the total time the gliding should take, and the number of steps to make during the glide. It should animate the mouse pointer by moving from (start x, start y) to (end x, start y) in n steps. The total glide should take t milliseconds.

I don't know how to get started can anyone help me get started on this?

Is This A Good Question/Topic? 0
  • +

Replies To: How to move a mouse by using java code?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,307
  • Joined: 21-June 11

Re: How to move a mouse by using java code?

Posted 21 February 2012 - 05:36 PM

You could start by writing a method that only moves on a horizontal line (i.e. it only takes x-coordinates, no y-coordinates) and ignores the time. I.e. it simply iterates from the starting X to the end X in n steps and calls mouseMove(x,42) each time.

Once you have that you can think about how you can add time to this. Just think about how long you have to wait after each move if you have n moves and the total time should be t.

Once you have that you can think about how to adjust you iteration logic for two dimensions.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to move a mouse by using java code?

Posted 21 February 2012 - 09:52 PM

View Postsepp2k, on 21 February 2012 - 07:36 PM, said:

and calls mouseMove(x,42) each time.

mouseMove() method of which class ?
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: How to move a mouse by using java code?

Posted 21 February 2012 - 10:06 PM

There is always Robot
Was This Post Helpful? 1
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,307
  • Joined: 21-June 11

Re: How to move a mouse by using java code?

Posted 22 February 2012 - 10:35 AM

View Postpbl, on 22 February 2012 - 05:52 AM, said:

View Postsepp2k, on 21 February 2012 - 07:36 PM, said:

and calls mouseMove(x,42) each time.

mouseMove() method of which class ?


Since the OP was the one who mentioned the mouseMove method, I assume he already knows which class it's defined in and how to call it.
Was This Post Helpful? 0
  • +
  • -

#6 dlee32695  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-February 12

Re: How to move a mouse by using java code?

Posted 22 February 2012 - 10:55 PM

View Postsepp2k, on 21 February 2012 - 05:36 PM, said:

You could start by writing a method that only moves on a horizontal line (i.e. it only takes x-coordinates, no y-coordinates) and ignores the time. I.e. it simply iterates from the starting X to the end X in n steps and calls mouseMove(x,42) each time.

Once you have that you can think about how you can add time to this. Just think about how long you have to wait after each move if you have n moves and the total time should be t.

Once you have that you can think about how to adjust you iteration logic for two dimensions.


how would you start writing a method that moves on a horizontal line and why would you call mouseMove(x,42)?

View Postfarrell2k, on 21 February 2012 - 10:06 PM, said:

There is always Robot

thats awesome but how would you use those methods and incorporate it into this problem. Because i saw the link you gave me but I dont know how to use it so that i write code because all I see is methods.
Was This Post Helpful? 0
  • +
  • -

#7 dlee32695  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-February 12

Re: How to move a mouse by using java code?

Posted 22 February 2012 - 11:09 PM

I need to make these arrays because I am trying to make a mouse glide from the first point to the second, the second to the third, and etc... Each glide should be broken into 25 steps and take 1000 milliseconds.

I don't know how to start the method exactly.

public void glide(int [] x, int [] y) I even have doubt that i set this method correctly. I really don't have a clue how to start this method.
Was This Post Helpful? 0
  • +
  • -

#8 dlee32695  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-February 12

Re: How to move a mouse by using java code?

Posted 22 February 2012 - 11:16 PM

how would you write a method that takes a starting x and starting y , and width. It should start at (x,y) and move a the pointer in a square with side-lengths width. Start moving right and go counter-clockwise. Each slide should be a glide with 20 steps and take 500 milliseconds. At each vertex of the square, it should click the mouse.

How do you create/ start creating this method. I have no clue on starting this method.
Was This Post Helpful? 0
  • +
  • -

#9 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: How to move a mouse by using java code?

Posted 22 February 2012 - 11:19 PM

Why don't you consolidate those 2 arrays into 1 and make it an array of Points?

You can even use an ArrayList if you do not know how many you will have.

Ex. -
public ArrayList<Point> coordinates = new ArrayList<Point>();
public void glide(Point coord)
{
    mouse.setPosition(coord.getX(), coord.getY());
]


Whatever your code is.

A Point has both an X and a Y coordinate so works perfectly for you. Can call the getX() and getY() methods to get the exact location on the axis.. don't forget to cast to an int as it returns a double
Was This Post Helpful? 1
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: How to move a mouse by using java code?

Posted 22 February 2012 - 11:19 PM

Please do not create 2 threads, You have an answer in your previous one. Be patient, you will be helped buddy.
Was This Post Helpful? 0
  • +
  • -

#11 dlee32695  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-February 12

Re: How to move a mouse by using java code?

Posted 22 February 2012 - 11:20 PM

View PostFuzzyness, on 22 February 2012 - 11:19 PM, said:

Why don't you consolidate those 2 arrays into 1 and make it an array of Points?

You can even use an ArrayList if you do not know how many you will have.

Ex. -
public ArrayList<Point> coordinates = new ArrayList<Point>();
public void glide(Point coord)
{
    mouse.setPosition(coord.getX(), coord.getY());
]


Whatever your code is.

A Point has both an X and a Y coordinate so works perfectly for you. Can call the getX() and getY() methods to get the exact location on the axis.. don't forget to cast to an int as it returns a double


ok but how would you move the pointer in a square with side length width?
Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: How to move a mouse by using java code?

Posted 22 February 2012 - 11:25 PM

Move the pointer? What pointer?

When you said "Mouse" in first post did you mean the Cursor?
Was This Post Helpful? 0
  • +
  • -

#13 dlee32695  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-February 12

Re: How to move a mouse by using java code?

Posted 22 February 2012 - 11:27 PM

View PostFuzzyness, on 22 February 2012 - 11:19 PM, said:

Please do not create 2 threads, You have an answer in your previous one. Be patient, you will be helped buddy.

I actually dont think i ment to do that.
Was This Post Helpful? 0
  • +
  • -

#14 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: How to move a mouse by using java code?

Posted 22 February 2012 - 11:29 PM

Well actually, you did because there is different information in this one than the other one. Numbers are different in the instructions that you gave.

500ms != 1000ms && 20 steps != 25 steps

happens, wait next time. Lets return to your other one and just let this slowly float away to be lost in the Ocean of Java threads.
Was This Post Helpful? 0
  • +
  • -

#15 dlee32695  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-February 12

Re: How to move a mouse by using java code?

Posted 22 February 2012 - 11:32 PM

View PostFuzzyness, on 22 February 2012 - 11:29 PM, said:

Well actually, you did because there is different information in this one than the other one. Numbers are different in the instructions that you gave.

500ms != 1000ms && 20 steps != 25 steps

happens, wait next time. Lets return to your other one and just let this slowly float away to be lost in the Ocean of Java threads.


I swear I didn't also what's the title of the question. Also I wrote multiple questions that are kind of similar to this but ask for different things because this questions relates to a bigger project.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2