diff --git a/src/java/org/lwjgl/opengl/AWTUtil.java b/src/java/org/lwjgl/opengl/AWTUtil.java index cc2ca85b..07456e3c 100644 --- a/src/java/org/lwjgl/opengl/AWTUtil.java +++ b/src/java/org/lwjgl/opengl/AWTUtil.java @@ -92,18 +92,25 @@ final class AWTUtil { } } - public static void setCursorPosition(final Component component, int x, int y) { + public static Robot createRobot(final Component component) { try { Robot robot = (Robot)AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws Exception { return new Robot(component.getGraphicsConfiguration().getDevice()); } }); + return robot; + } catch (PrivilegedActionException e) { + LWJGLUtil.log("Got exception while creating robot: " + e.getCause()); + return null; + } + } + + public static void setCursorPosition(Component component, Robot robot, int x, int y) { + if (robot != null) { int transformed_x = component.getX() + x; int transformed_y = component.getY() + component.getHeight() - 1 - y; robot.mouseMove(transformed_x, transformed_y); - } catch (PrivilegedActionException e) { - LWJGLUtil.log("Got exception while setting mouse cursor position: " + e); } } diff --git a/src/java/org/lwjgl/opengl/AbstractAWTInput.java b/src/java/org/lwjgl/opengl/AbstractAWTInput.java index 69967c41..fe7c462f 100644 --- a/src/java/org/lwjgl/opengl/AbstractAWTInput.java +++ b/src/java/org/lwjgl/opengl/AbstractAWTInput.java @@ -38,6 +38,7 @@ import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLUtil; import java.awt.Cursor; +import java.awt.Robot; /** * @@ -47,6 +48,7 @@ import java.awt.Cursor; */ abstract class AbstractAWTInput implements AWTCanvasInputImplementation { private AWTGLCanvas canvas; + private Robot robot; private KeyboardEventQueue keyboard_queue; private MouseEventQueue mouse_queue; @@ -54,6 +56,7 @@ abstract class AbstractAWTInput implements AWTCanvasInputImplementation { protected AbstractAWTInput(AWTGLCanvas canvas) { this.canvas = canvas; + this.robot = AWTUtil.createRobot(canvas); } protected synchronized MouseEventQueue getMouseEventQueue() { @@ -117,7 +120,7 @@ abstract class AbstractAWTInput implements AWTCanvasInputImplementation { } public void setCursorPosition(int x, int y) { - AWTUtil.setCursorPosition(canvas, x, y); + AWTUtil.setCursorPosition(canvas, robot, x, y); } public void setNativeCursor(Object handle) throws LWJGLException { diff --git a/src/java/org/lwjgl/opengl/MacOSXDisplay.java b/src/java/org/lwjgl/opengl/MacOSXDisplay.java index 1837d15c..646c0e12 100644 --- a/src/java/org/lwjgl/opengl/MacOSXDisplay.java +++ b/src/java/org/lwjgl/opengl/MacOSXDisplay.java @@ -69,6 +69,7 @@ final class MacOSXDisplay implements DisplayImplementation { private static final int GAMMA_LENGTH = 256; private MacOSXFrame frame; + private Robot robot; private MacOSXMouseEventQueue mouse_queue; private KeyboardEventQueue keyboard_queue; private java.awt.DisplayMode requested_mode; @@ -85,6 +86,7 @@ final class MacOSXDisplay implements DisplayImplementation { close_requested = false; try { frame = new MacOSXFrame(mode, requested_mode, fullscreen, x, y); + robot = AWTUtil.createRobot(frame); } catch (LWJGLException e) { destroyWindow(); throw e; @@ -319,7 +321,7 @@ final class MacOSXDisplay implements DisplayImplementation { } public void setCursorPosition(int x, int y) { - AWTUtil.setCursorPosition(frame, x, y); + AWTUtil.setCursorPosition(frame, robot, x, y); } public void setNativeCursor(Object handle) throws LWJGLException {