This discussion is archived
4 Replies Latest reply: Jun 23, 2010 11:49 AM by DarrylBurke RSS

Robot.mouseMove &  Mouse-Motion Listener problem

843807 Newbie
Currently Being Moderated
Hi there!

Here is my problem :

I am using a Mouse-Motion Listener. Thanks to this listener, I can catch the movements of the mouse. I have implemented the “mouseMoved(MouseEvent e)” function.

I am using, in my function “mouseMoved(MouseEvent e)”, a Robot (java.awt.Robot) object. I use the method “mouseMove(int x, int y)” of this object to replace the pointer of the mouse (it's the general purpose of my program).

My problem is that this movement created by “mouseMove(int x, int y)” is handled as a normal mouse event by the function “mouseMoved(MouseEvent e)” so these two functions start an “infinite loop”.

Do you know how I can proceed to avoid this “infinite loop” ?

Thank you for your help,
Sam
  • 1. Re: Robot.mouseMove &  Mouse-Motion Listener problem
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Two possible approaches:

    Before the robot.mouseMove, set a boolean flag. After the mouseMove, reset the flag. In the mouseMoved, check the flag and do something or nothing accordingly.

    OR

    Before the mouseMove, remove the MouseMotionListener, and add it back after the mouseMove.
    these two functions start an “infinite loop”.
    Does that mean you are invoking mouseMove from mouseMoved? Sounds like a poor, or at best confusing, design to me.

    To get better help sooner, post a [_SSCCE_|http://mindprod.com/jgloss/sscce.html] that clearly demonstrates your problem.

    Use code tags to post codes -- [code]CODE[/code] will display as
    CODE
    Or click the CODE button and paste your code between the {code} tags that appear.

    db
  • 2. Re: Robot.mouseMove &  Mouse-Motion Listener problem
    843807 Newbie
    Currently Being Moderated
    Thank you for your help. I've tried the first approach you suggested me but I still have the same problem even if I create a special "synchronized" function to manage the flag.

    In order to understand well the source code, I would like to explain that the general purpose of my code is to create a virtual cursor which allows off-screen features. To give the coordinates to this virtual cursor, I have 2 variables and I compute the Delta X and Delta Y of a movement of the real cursor. After the movement (only a few pixel move), I would like to replace the cursor (real one) in the middle of the screen, that's why I use the Robot.mouseMove method.

    Here is the relevant part of my source code :
    public class MouseMoveListener extends JCanvasMouseAdapter {
    
         private JCanvas jc;
         private Boolean replaceON;
         private int i = 0;
    
         public void mouseMoved(MouseEvent e) {
              
              if(this.replaceON == false) {
              
                   // x_old and y_old are the old position of the cursor
                   double x_old;
                   double y_old;
                   
                   // x_old and y_old are the new positions of the cursor
                   double x_new;
                   double y_new;
                   
                   // We collect the screen resolution of the user.
                   int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
                   int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
                   
                   // x_old and y_old are the old position of the cursor
                   x_old = e.getLocationOnScreen().getX();
                   y_old = e.getLocationOnScreen().getY();
              
                   // It will provide the Point that represents the coordinates of the pointer.
                   PointerInfo pointer = MouseInfo.getPointerInfo();
                   
                   // Location is the current position of the cursor. 
                   Point location = pointer.getLocation();
                   
                   // x_new and y_new are the new positions of the cursor
                   x_new = location.getX();
                   y_new = location.getY();
    
                   // If positions are different, we update the new position of the pointer.
                   if(x_old != x_new || y_old != y_new) {
                   
                   // We update the position of the virtual mouse
                   VirtualMouse.virtualMouse.x += (x_new - x_old);
                   VirtualMouse.virtualMouse.y += (y_new - y_old);
                   
                   // We update the old position of the virtual mouse
                   VirtualMouse.virtualMouse_old.x = (int) (VirtualMouse.virtualMouse.x - (x_new - x_old));
                   VirtualMouse.virtualMouse_old.y = (int) (VirtualMouse.virtualMouse.y - (y_new - y_old));
                   
                   // Is the cursor outside the screen ?
                   if(VirtualMouse.virtualMouse.x<0 || VirtualMouse.virtualMouse.x>screenWidth || VirtualMouse.virtualMouse.y<0 || VirtualMouse.virtualMouse.y>screenHeight) {
                        VirtualMouse.cursorOutside = true;
                   }
                   else {
                        VirtualMouse.cursorOutside = false;
                   }
                   
                   // The normal cursor is always in the center of the screen
                   replaceCursor();
                   
                   // Allow to see the number of invocation (infinite loop)
                   System.out.println("iteration n° "+i);
                   i++;
                             
                   jc.repaint();
              }
         }
         
         private synchronized void replaceCursor() {
              this.replaceON = true;
              // Constructs a Robot object in the coordinate system of the primary screen.
              Robot robot;
              try {
                   robot = new Robot();
                   // The normal cursor is always in the center of the screen
                   robot.mouseMove(Toolkit.getDefaultToolkit().getScreenSize().width/2, Toolkit.getDefaultToolkit().getScreenSize().height/2);
              } catch (AWTException e) {
                   e.printStackTrace();
              }
              this.replaceON = false;
         }
    }
    The fact is that "iteration n° xx" appears permanently (infinite loop) even if the mouse doesn't move.

    Sam.

    Edited by: grinko4 on Jun 14, 2010 10:25 PM
  • 3. Re: Robot.mouseMove &  Mouse-Motion Listener problem
    843807 Newbie
    Currently Being Moderated
    Before the robot.mouseMove, set a boolean flag. After the mouseMove, reset the flag. In the mouseMoved, check the flag and do something or nothing accordingly.
    OR

    Before the mouseMove, remove the MouseMotionListener, and add it back after the mouseMove.>

    None of these two approaches solve my problem ... each one slow down considerably my program.

    In a more general way, I would like to know if it's possible to use the java.awt.Robot.mouseMove() function without generating a java.awt.event.MouseEvent
  • 4. Re: Robot.mouseMove &  Mouse-Motion Listener problem
    DarrylBurke Guru Moderator
    Currently Being Moderated
    None of these two approaches solve my problem ... each one slow down considerably my program.
    Post a [SSCCE (Short, Self Contained, Compilable and Executable, Example Program)|http://mindprod.com/jgloss/sscce.html] that demonstrates that.
    In a more general way, I would like to know if it's possible to use the java.awt.Robot.mouseMove() function without generating a java.awt.event.MouseEvent
    No.

    db