Jump to content

Move cursor with java.awt.robot


xoombie

Recommended Posts

Developing a mod with 1.15.2, am getting

java.awt.HeadlessException

from 

PointerInfo pointerInfo = MouseInfo.getPointerInfo();

Have been looking at this excellent example: motecraft, although it is out of date, so for 1.15.2 am using the following:

DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> {
    initRobot();
});

void initRobot() {

    try {
        this.robot = new Robot();
    } catch (AWTException e){
        e.printStackTrace();
    }

    PointerInfo pointerInfo = MouseInfo.getPointerInfo();

	DisplayMode mode = pointerInfo.getDevice().getDisplayMode();

	int width = mode.getWidth();
    int height = mode.getHeight();

    this.displayPixels = height < width ? height : width;
}

Have tried many, many variations, but no way to avoid the exception. 

Can anyone indicate whether the Minecraft 1.15.2 client is headless? Or is there something obvious I've missed.

Any info greatly appreciated.

Link to comment
Share on other sites

Finally cracked it. In case useful to anyone else, in the client you have to manually clear the headless state:

MyClassConstructor() {

    // ....

    DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> {
        initRobot();
    });
}

void initRobot() {

    System.setProperty("java.awt.headless", "false");

    try {
        this.robot = new Robot();
    } catch (AWTException e){
        e.printStackTrace();
    }

    PointerInfo pointerInfo = MouseInfo.getPointerInfo();

    // you can now use all the usual Robot class methods

}

 

  • Like 1
Link to comment
Share on other sites

  • 9 months later...
  • Guest locked this topic
Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.