Mouse dragging in JME3


I’m trying to implement an RTS-like selection boxes, but I’ve got stuck when trying to implement mouse dragging, so that I can draw the rectangle on the screen.

First, I registered the following mappings:

[java]this.inputManager.addMapping(“Pick”, new MouseButtonTrigger(MouseInput.
new MouseAxisTrigger(MouseInput.AXIS_X, true),
new MouseAxisTrigger(MouseInput.AXIS_X, false),
new MouseAxisTrigger(MouseInput.AXIS_Y, true),
new MouseAxisTrigger(MouseInput.AXIS_Y, false)

And here’s my input listener code:

[java]private static class ApplicationInput implements AnalogListener,
ActionListener {

private final SimpleApplication app;
private boolean dragging = false;
private Vector2f dragStart;

public ApplicationInput(final SimpleApplication application) { = application;

public void onAnalog(String name, float value, float tpf) {
    switch (name) {
        // .. omitted ..
        case "PickMouseMove": {
            if (this.dragging) {
                // Here's where it gets weird.
                System.out.printf("%s - %s\n", this.dragStart.toString(),

public void onAction(String name, boolean isPressed, float tpf) {
    if (name.equals("Pick")) {
        if (!isPressed) {
            this.dragging = false;
            // .. omitted ..
        } else {
            if (!this.dragging) {
                this.dragging = true;
                this.dragStart = app.getInputManager().


I noticed that getCursorPosition() always returns the same coordinates, even though the mouse is moving.

Do you have any idea why the above is happening? I suspect that I may be using the listeners wrong, please let me know if that’s the case.

Thank you,

It’s because the cursor is not visible. If you want to use the cursor for positioning then it needs to be visible… and then it’s better to just register a RawInputListener to get actual MouseEvents.

If you instead want to drag with the center of the screen (which is currently how you are setup) then you need to detect the motion yourself.

1 Like

Thanks a lot, I think that a RawInputListener is what I’m looking for.