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) {
}
}