how to restrict mouse moving area?

hi,
i know this post doesn’t really match in Java2D but i didn’t know where to put it else!

my question is:
is it possible to restrict the mouse moving area? if yes, how?
for example that the mouse pointer can’t go outside the window, or even better, that the mouse pointer can’t go outside a specific box inside the frame.
And how to swap between “restricted mode” and “free mode”?

have a look at java.awt.Robot

nice idea, but doesn’t fit very well.
When i put conditions near the window borders to keep it inside, the cursor can still go out of the window if it is moved fast enough.
any other solution?

it should do the trick… how are you using it? you are using it together with a MouseMotionListener right?

yes, exactly.

i put “conditions” in the MouseMotionListener like:
if (e.getX() < 10)
myRobot.mouseMove(10, e.getY());

Should i use it another way?

hmm it depends on the resolution of the mouse i guess. this is the same problem found in games. if the movement is jerky then a car for instance can drive through a wall.

the trick is to see where the cursor is going to be next, that is getX()+dx. save the old getX in a variable and calc dx and then use that in the if-condition.

i haven’t used Robot in a long time and it worked pretty well, but this should do the trick. anyone else has any ideas?

forgot to mention that when you set it to a new position, remember to substract the speed which you moved the mouse, as in maxX-dx;

thanks for the idea, but it is impossible to know. i don’t get the x and y variations but only the absolute pos.
And when you go fast enough, you can go from the pos (50, y) to ‘out of frame’ in one move.
So the solution isn’t working fine.

It could maybe work if i knew how to get the absolute mouse coord (outside the window), but i don’t know if it’s possible either.
Lastly, since it’s not really the purpose of ‘Robot’, it wouldn’t be a very nice solution…

Maybe there is another simplier solution… i hope.
thanks anyway.

has anyone another solution?

well, if you save your last pos in lastX, then dx would be getX()-lastX, as simple as that. it should work.

anyway, i’ll think about some other solution for a while.

Why do you care that the mouse goes outside a certain area if you can still “put it back” with the Robot as when you find out?
If it is just the visual distraction of the pointer going outside your bounds, then maybe you can work around it by using a custom cursor to hide the pointer completely, then draw your own cursor that you restrict to the boundary area in your own drawing code.
Anyway give a bit more info about what end effect you are after and maybe we can come up with more ideas.

yup, i’m puzzled as well. what i suggested is what i believe the normal way to do it and it works for me.

a common thing is that people don’t “close” a thread, as in saying “i managed to get things working, thanks for your help”. it’s much easier to not to post anything.

totally unrelated to java, but…

Quake3 when played in a window has the annoying habit of occasionally losing the focus. >:(

Ive put it down to the mouse momentarily getting outside the frames borders at the exact same time as a mouse click occuring. :o

Heh, that’s pretty funny. I’ve become a bit peeved when accidentally clicking a different window in XBoing and having to rapidly switch between windows before the ball comes down again, but I expect the same happening in Quake3 would turn the air blue… ;D