We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › awt.Robot class and mouseX, mouseY delay
Page Index Toggle Pages: 1
awt.Robot class and mouseX, mouseY delay (Read 966 times)
awt.Robot class and mouseX, mouseY delay
Nov 27th, 2007, 11:02am
 
Hi,

I'm doing a project where I wish to keep the mouse pointer within the applet window. For this i'm using the awt.Robot class, but whenever I do robot.mouseMove() the mouseX and mouseY doesn't update for around 300 ms eventhough the user keeps moving the mouse. After this delay the mouseX updates normally.

I've made a test example illustrating the problem.. see below..

I know from the faq (http://processing.org/faq.html#java) that there are problems with the awt, but since many posts on this forum refer to the robot class I figured there must be some way to make it work.

I hope someone can help me with this.

I'm using 0135.

_prinds

Code:

import java.awt.*;
import javax.swing.SwingUtilities;

int pmousex = 0, mouseMovement = 0;
boolean centering = false;

PFont font;

void setup()
{
size(600,600);
font = loadFont( "AbadiMT-CondensedExtraBold-48.vlw" );
}

void draw()
{
background( 205 );
rect( 0,0, 50,height );
rect( width-50,0, width,height );

textFont( font );
textAlign(CENTER);
text( mouseMovement, width*0.5, height*0.5 );

println( "reading mousemovement : millis = " + millis() + ", mouseX = " + mouseX );
mouseMovement = 0;
}

void mouseMoved()
{
if( centering ) // don't to add the robot mouse movement to 'mouseMovement'
{
centering = false;
}else{ // add normal mouse movement to 'mouseMovement'
mouseMovement += mouseX - pmousex;
if( mouseX < 50 || mouseX > width-50 ) // if cursor is at left or right side of the window, move it to center
{
centering = true;
println("centering : millis = " + millis() + ", mouseX = " + mouseX);
moveCursor();
}
}
pmousex = mouseX;

}

void moveCursor()
{
try {
// Move the cursor
Robot robot = new Robot();
Point p = new Point( int( width*0.5 ), int( height*0.5 ) );
SwingUtilities.convertPointToScreen( p, this );
robot.mouseMove( p.x, p.y ); // if you remove this line, the will be no delay
}
catch (AWTException e) {
}
}
Page Index Toggle Pages: 1