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 & HelpSyntax Questions › code driven mouse movement
Page Index Toggle Pages: 1
code driven mouse movement (Read 1200 times)
code driven mouse movement
Feb 15th, 2010, 4:02am
 
hey there,

i was wondering if i can move the mouse without physically moving it, just with code. maybe by accessing mouseX and mouseY directly.
so that the cursor moves across the screen by itself.
is that possible?

thank you!

Re: code driven mouse movement
Reply #1 - Feb 15th, 2010, 4:07am
 
This is not so easy... Search for the term "robot", this is how automation is called in Java :

http://processing.org/hacks/hacks:robot
http://processing.org/discourse/yabb2/?num=1250869102
Re: code driven mouse movement
Reply #2 - Feb 15th, 2010, 7:37am
 
thank you antiplastik, i got this robot thing working to change mouseX and mouseY for me.

but is there a way to differentiate between code driven mouse movement and regular physical mouse movement? because i only want the robotic mouse to move when the mouse hasn't been physically touched for some time.
the mouseMoved method which i had in mind for that reacts to both mouse movements the same.

Re: code driven mouse movement
Reply #3 - Feb 15th, 2010, 8:26am
 
Set a flag telling you are moving the mouse yourself.
Re: code driven mouse movement
Reply #4 - Feb 15th, 2010, 8:30am
 
but how can i trigger that flag automatically? i need the program to see by itself who moves the mouse (me or robot)...
Re: code driven mouse movement
Reply #5 - Feb 15th, 2010, 9:02am
 
Well you can set a flag when you know the Robot is moving the mouse (i.e. when robot.mouseMove is invoked) and otherwise assume the user is moving it.  However I suspect the reason for your follow-up question is that you want the user to be able to move the mouse and for this to release it from the Robot?  That looks quite tricky...

I just had a quick look and I don't see how it could easily be done: the mouseEvent is triggered by the sketch, so that's no help.  You'd presumably have to get at the mouse input at a lower level and see whether this input corresponds to that in the mouseMoved event - i.e. if mouseMoved is registering movement but the mouse isn't sending any data you know the robot is the cause.

I suspect it would be far simpler to use a key press to release the mouse, as in the example sketch.  Still I'm not a Java expert so maybe someone will have an answer...  Or maybe I've misunderstood the question?
Re: code driven mouse movement
Reply #6 - Feb 15th, 2010, 9:58am
 
you got it totally right blindfish, sorry if i expressed myself unprecise.

i have made some mouse reactive particle simulations, that i need to prepare for a semester presentation. but since it is very unlikey that there will be all the time someone operating the mouse and the particle simulations look quite stupid without mouse reaction, so i need something like:
if the mouse is moved by someone run the program just normal
if the mouse hasn't been moved by someone for some time let the robot move it
if the mouse is moved again by someone shut down the robot

since everyone should be able to just grab the mouse and move it a keypressed trigger would not be a good solution.
from just thinking about it, getting the input at a lower level seems promising, but probably exceeds my non-existing java skills.
Re: code driven mouse movement
Reply #7 - Feb 15th, 2010, 2:18pm
 
Even at system level, it is nearly impossible to tell if mouse movement was made by user or by a robot: see all the confirmation dialogs in Vista and Win7 when you start an action... All the mouse info comes from the driver, so the system can't tell if hardware was behind this.

It might be much simpler to adapt your script to answer to an arbitrary couple of coordinates: if mouse moves, it is called with mouseX/mouseY, otherwise, it is called with some random (Brownian?) coordinate.
Re: code driven mouse movement
Reply #8 - Feb 15th, 2010, 8:51pm
 
Answer: don't move the system caret (the mouse).

Instead of using mouseX and mouseY directly for your control method(s), create your own "pointer", which can be updated by mouseMoved() in your sketch (actual user input) as well as by some other code when there has been no user activity for some time.

You might want to call noCursor() to hide the cursor when running in automatic mode.

Idea (untested:)

Code:

final long TIMEOUT = 5 * 60 * 1000; //  5 minutes in milliseconds
long lastMovement = millis(); // Time of last mouse movement
int pointerX, pointerY; // replacement for mouseX, mouseY

void mouseMoved()
{
 pointerX = mouseX;
 pointerY = mouseY;
 lastMovement = millis();
}

draw()
{
 if (millis() - lastMovement > TIMEOUT)
 {
   // insert automatic control of pointerX, pointerY here
 }

 // use pointerX, pointerY to drive your program
}


-spxl
Re: code driven mouse movement
Reply #9 - Feb 16th, 2010, 12:13am
 
monsieurBlutbad wrote on Feb 15th, 2010, 9:58am:
you got it totally right blindfish, sorry if i expressed myself unprecise.

i have made some mouse reactive particle simulations, that i need to prepare for a semester presentation. but since it is very unlikey that there will be all the time someone operating the mouse and the particle simulations look quite stupid without mouse reaction, so i need something


This is a little unrelated, but if you just need a "ghost" for a presentation, you might want to look into separte software "automating" any user input on an OS. I've made very good experience with a (free) software called AutoIT.
Page Index Toggle Pages: 1