How to move th emouse from the program

Hello experts:

I am trying to write a small test program to move the mouse from the program instead of moving the mouse by holding the mouse.

In the following program I have written, I create a mouseEvent and it traps the mouseMoved event. But i do not see teh mouse actually moves on the screen. Can anyone give me a hint?

import java.awt.image.*;
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
 
  public class MouseMover extends JFrame implements MouseMotionListener
  {
   Timer timer;
   int count = 0;
 
   public MouseMover( String s )
   {
     // call base class constructor
      super( s );
      setSize( 350, 250);
      addWindowListener( new CloseWindowAndExit() );
      addMouseMotionListener(this);
      timer = new Timer(1000, new TimerListener());
      timer.start();
   }
 
   class TimerListener implements ActionListener
   {
    public void actionPerformed(ActionEvent evt)
    {
     ++count;
     System.out.println("Hit Number : " + count);
     moveTheMouse();
    }
   }


  private void moveTheMouse()
  {
   Dimension dm = Toolkit.getDefaultToolkit().getScreenSize();
   int XX = (int)dm.getWidth();
   int YY = (int)dm.getHeight();
   int X, Y;
   do{
    X = (int)(Math.random() * 100);
   }while (X < 0 && X > XX);

   do{
    Y = (int)(Math.random() * 100);
    } while (Y < 0 && Y > YY);

   System.out.println("Width : " + X + " Height : " + Y);

   processMouseMotionEvent(new MouseEvent(this,
                                          MouseEvent.MOUSE_MOVED,
                                          count *1000,
                                          0,
                                          X,
                                          Y,
                                          0,
                                          false));
  }


  public void mouseMoved(MouseEvent e)
  {
   System.out.println("Current Location : ("+e.getX()+","+e.getY()+")");
   System.out.println("Modifiers Flag : " +   e.getModifiers());  
  }

  public void mouseDragged(MouseEvent e)
  {
  }

  public static void main( String args[] )
  {    
   MouseMover f = new MouseMover ("Moving Mouse");
   f.setVisible(true);
  }
 
 }
prainAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prainAuthor Commented:
Edited text of question.
0
JodCommented:
you cannot do this in pure Java - you will need a native system call to move the cursor.

For example:

http://forum.java.sun.com/forum?13@147.W8KhaqqFaqT^0@.eed7d23/0

Replace your move the mouse routine with this to see what I mean:

  private void moveTheMouse()
  {
   Dimension dm = Toolkit.getDefaultToolkit().getScreenSize();
   int XX = (int)dm.getWidth();
   int YY = (int)dm.getHeight();
   int X, Y;
   do{
    X = (int)(Math.random() * 100);
   }while (X < 0 && X > XX);

   do{
    Y = (int)(Math.random() * 100);
    } while (Y < 0 && Y > YY);

     System.out.println("Width : " + X + " Height : " + Y);

   if (this.getCursor().getType() == Cursor.DEFAULT_CURSOR)
     this.setCursor ( new Cursor(Cursor.HAND_CURSOR) );
   else
     this.setCursor (new Cursor(Cursor.DEFAULT_CURSOR) );

     processMouseMotionEvent(new MouseEvent(this,
                                            MouseEvent.MOUSE_MOVED,
                                            count *1000,
                                            0,
                                            X,
                                            Y,
                                            0,
                                            false));
  }

To change the cursor, you need to get hold of a cursor object and replace the current cursor with the new one, as the above code does to change the cursor shape. However, Cursor has no method to move the on screen cursor, so this is unfortunately this is not possible...

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prainAuthor Commented:
Jod:
I do not want to change the cursor shape. All I want to do is to move the default cursor to the new (X,Y) location. This I want to do within the program. Basically I do not want to touch the mouse. As you can see I an creating different (X,Y) locations randomly. I want to move the cursor these locations.
0
JodCommented:
I know. Look at the answer again...

I was just showing you what you can and cannot do with cursors and how to do it.

If you look at the cursor object you will see it has no facility to move the cursor, only to change it.

So basically, you can't move the cursor.

What you are doing is generating a dummy message to give a new cursor position - but the generation of such an event does not actually move the cursor. It has no way to do so.

As I showed, it could change the cursor...but not move it.

Sorry, but that's it for this version of Java. I believe Java 1.3 has the ability to do this, but it would mean working with a beta version of the JDK.

Otherwise you will have to write a native method to hook into the operating system and move the cursor. Not as daunting as it sounds, but not trivial either.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.