Sign in to follow this  
EGD Eric

[java] how do you set the cursor position?

Recommended Posts

I'd like to be able to set the cursor position to be in the middle of the screen all the time, or maybe at 0, 0. Problem is, I can't find a function for setting the cursor position! (unlike SetCursorPos in Win32)

Share this post


Link to post
Share on other sites
argh, its no good. robot.mouseMove actually generates a mouseMove event (go figure). I want my spaceship to turn with the mouse, but for the mouse's position to be reset to the center every time a mouseMove was detected. The very fact that mouseMove generates a mouseMove event means that all movement I try to do with the ship gets cancelled.

Share this post


Link to post
Share on other sites
Quote:
Original post by EGD Eric
argh, its no good. robot.mouseMove actually generates a mouseMove event (go figure). I want my spaceship to turn with the mouse, but for the mouse's position to be reset to the center every time a mouseMove was detected. The very fact that mouseMove generates a mouseMove event means that all movement I try to do with the ship gets cancelled.


Seems like you could programatically intercept that..


class MyMouseClass
{
int previousMouseX = 0;
int previousMouseY = 0;
int myMouseMovementX = 0;
int myMouseMovementY = 0;

void mouseMoved(MouseEvent e)
{
int dist_x = e.getX() - previousMouseX;
int dist_y = e.getY() - previousMouseY;
previousMouseX = e.getX();
previousMouseY = e.getY();

// check to intercept mouse movement that we caused
if (dist_x == myMouseMovementX && dist_y == myMouseMovementY)
{
myMouseMovementX = 0;
myMouseMovementY = 0;
return;
}

// go handle the mouse movement
doWhateverWithMouseMovement(dist_x, dist_y);

// reset the mouse location
int distToCenterX = centerX - e.getX();
int distToCenterY = centerY - e.getY();
myMouseMovementX = distToCenterX;
myMouseMovementY = distToCenterY;
Robot.mouseMove(distToCenterX, distToCenterY);
}
}

Share this post


Link to post
Share on other sites
programatically eh? I tried that, and it didn't work. Using the quick & dirty naive way that I was trying to do it. (with a boolean). your idea Pinacolada, works. That JInput thing looks interesting though.

Share this post


Link to post
Share on other sites
Could you not do something like.


class MyMouseClass
{
boolean isReseting;

void resetMouse()
{
isReseting = true;
Robot.mouseMove(0,0);
isReseting = false;
}

void mouseMoved(MouseEvent e)
{
if(isReseting)
return;

// TO DO
}
}


Share this post


Link to post
Share on other sites
lol, that's the 'naive' way that I originally tried! Seriously though, you'd think that would work, but it doesn't.


Edit: Ok, I know I said it works, but when I test it using a web browser, it doesn't. According to the console, I don't have permission to use the robot. This must be some security thing to prevent people from hijacking computers using java.



Java Plug-in 1.5.0_06
Using JRE version 1.5.0_06 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Eric Cote



Quote:

java.security.AccessControlException: access denied (java.awt.AWTPermission createRobot)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.awt.Robot.checkRobotAllowed(Unknown Source)
at java.awt.Robot.init(Unknown Source)
at java.awt.Robot.<init>(Unknown Source)
at SpaceSoccer.init(SpaceSoccer.java:120)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "thread applet-SpaceSoccer.class" java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


[Edited by - EGD Eric on November 1, 2006 12:38:00 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this