[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

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);
  }
 
 }
0
prain
Asked:
prain
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now