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 & HelpPrograms › How to get mouse coordinates outside the frame
Page Index Toggle Pages: 1
How to get mouse coordinates outside the frame? (Read 578 times)
How to get mouse coordinates outside the frame?
Feb 11th, 2009, 2:15am
 
I came across this piece of code:
http://processing.org/hacks/hacks:clickedmousepos

I tried implementing it into a sketch:

void setMouseXY()
{
 if(mouseX>=0 && mouseX<width && mouseY>=0 && mouseY<height) return;
 Point mouse, winloc;
 mouse = MouseInfo.getPointerInfo().getLocation();
 winloc = frame.getLocation();
 if(!frame.isUndecorated()){
   winloc.x += 3;
   winloc.y += 29;
 }
 mouseX = mouse.x-winloc.x;
 mouseY = mouse.y-winloc.y;
}

void setup()
{
 size(400, 400, P3D);
}

void draw()
{
 setMouseXY();
 println("\nx: " + mouseX + " y: " + mouseY);

}

When I try to compile, it gives the error:
Semantic Error: No accessible field named "MouseInfo" was found in type "Temporary_6179_3999".

A search on "No accessible field" led me to:
http://mobile.processing.org/discourse/YaBB.cgi?board=syntax;action=display;num=1177102386

According to that site, this error is triggered when trying to use a variable out of scope.  I'm not sure how I can get MouseInfo to be in scope, though, I'm not familiar with Java.

Any advice on how I can get this code to work, or achieve the same effect through another means, would be appreciated.
Re: How to get mouse coordinates outside the frame
Reply #1 - Feb 11th, 2009, 1:59pm
 
The "Temporary_6179_3999" part makes me think you have an old version of Processing.
MouseInfo have been introduced in Java 1.5, so your old copy of Processing might not support it.

Beside, I dislike this hack because:
- it changes a Processing variable that is meant to be read-only (just use another global variable);
- it hard-codes border and title sizes, which breaks on Windows with another look, or on another system.

You can get real sizes with Insets, as I show in problem resizing a window on the fly.
Re: How to get mouse coordinates outside the frame
Reply #2 - Feb 12th, 2009, 3:58am
 
Thank you for your help!  You were right about the version of Processing, turns out I had an old one.  Updating fixed the compile/run issue.

I wasn't aware of that mouseX/mouseY were meant to be read-only, I'll keep that in mind.  I'm guessing that may cause me trouble later on when trying to use Robot.  I'm assuming the variables aren't read-only yet, but will be sometime in the future?  Because the code does work, which I'm guessing it wouldn't if the variables were read-only.

Regarding the hard-coded border/title sizes, that Insets example looks like it will be very useful, and I would eventually have wanted to resize a window on the fly as well.

Thanks again, I should be able to move on to the fun parts of my project after getting the input out of the way, and I'm looking forward to that.
Re: How to get mouse coordinates outside the frame
Reply #3 - Feb 12th, 2009, 11:59am
 
Note: in Java, you cannot really make variables read-only, unless masking them -- making them private -- and providing only a getter -- a function to read them -- a solution Processing authors rejected in favor of the simpler direct access to variables.
Something that might make object oriented purists cringe, but Processing isn't really targeting them anyway! :-D

And actually, changing mouseX/Y probably doesn't break anything (inside Processing), but is brittle: value can be overwritten on next mouse event without advice, etc. It is better design to have your own, separated variables.
Page Index Toggle Pages: 1