Hardware mouse problem, button not released?

I'm having some problem using the hardware mouse in JME.

The following procedure gives me the unexpexted result:

  1. Press down a mouse button inside the JME window
  2. Hold the button and move the cursor outside the window
  3. Release the button
  4. Move back in to the window
  5. PROBLEM: JME thinks the button is still pressed

    How do I solve it?

    Below is a simple test case (a little change in the TestHardwareMouse.java only). ( you need to press G for hardware mouse as in the original example)

package jmetest.input;

import java.util.logging.Level;
import java.util.logging.Logger;

import jmetest.renderer.TestEnvMap;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.input.AbsoluteMouse;
import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.MouseInput;
import com.jme.input.action.InputAction;
import com.jme.input.action.InputActionEvent;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Controller;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.shape.Box;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.LightState;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;

 * <code>TestHardwareMouse</code>
 * @author Rikard Herlitz (MrCoder)
public class TestHardwareMouse extends SimpleGame {
    private static final Logger logger = Logger
   private AbsoluteMouse mouse;
   private Box spinningBox;
   private boolean useHardwareCursor;
   private Text currentTypeText;
   private final long SLEEPING_TIME = 20;

   public static void main(String[] args) {
      TestHardwareMouse app = new TestHardwareMouse();

   protected void simpleUpdate() {
      //Switch between software/hardware mousecursor
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("g", false)) {
         useHardwareCursor = !useHardwareCursor;
         if (useHardwareCursor) {
            //currentTypeText.print("Current cursor type: [HARDWARE]");
            //correction due to different hotspot positions between hardware/software. not needed when using only hardware etc.
            MouseInput.get().setCursorPosition((int) mouse.getLocalTranslation().x - mouse.getImageWidth() / 2,
                                       (int) mouse.getLocalTranslation().y + mouse.getImageHeight() / 2);
         else {
            //currentTypeText.print("Current cursor type: [SOFTWARE]");

      //Sleep to simulate game fps
      try {
      } catch (InterruptedException e) {
         logger.logp(Level.SEVERE, this.getClass().toString(), "simpleUpdate()", "Exception",

   protected void simpleInitGame() {
      input = new InputHandler();

      cam.getLocation().set(0, 0, 150);

      //Setup software mouse
      mouse = new AbsoluteMouse("Mouse Input", display.getWidth(), display.getHeight());
      TextureState cursor = display.getRenderer().createTextureState();
            Texture.MM_LINEAR, Texture.FM_LINEAR));
      AlphaState as1 = display.getRenderer().createAlphaState();

      //Setup dummybox to show that we are running
      spinningBox = new Box("SpinBox", new Vector3f(), 10, 10, 10);
      spinningBox.setModelBound(new BoundingBox());
      TextureState ts = display.getRenderer().createTextureState();
      Texture t0 = TextureManager.loadTexture(
      //Make the box spin around
      spinningBox.addController(new Controller() {
         float spinTime = 0;
         public void update(float time) {
            spinTime += time;
            spinningBox.getLocalRotation().fromAngles(spinTime, spinTime, spinTime);

        //create an action to shown button activity
        InputAction buttonAction = new InputAction() {
            public void performAction( InputActionEvent evt ) {
                String actionString;
                if ( !evt.getTriggerAllowsRepeats() ) {
                    actionString = evt.getTriggerPressed() ? "pressed" : "released";
                } else {
                    actionString = "down";
                currentTypeText.print( evt.getTriggerDevice() + " " + evt.getTriggerName() + " (" + evt.getTriggerIndex() + ":" + evt.getTriggerCharacter() + ") " +
                        actionString + " on " + timer.getTime() );
        //register the action with all devices (mouse, keyboard, joysticks, etc) for all buttons
        input.addAction( buttonAction, InputHandler.DEVICE_ALL, InputHandler.BUTTON_ALL, InputHandler.AXIS_NONE, false );
      //Setup keybinding and help text
      KeyBindingManager.getKeyBindingManager().set("g", KeyInput.KEY_G);

      Text helpText = new Text("Text", "Using forced sleep(" + SLEEPING_TIME + ") to clearly show the difference...");
      helpText.setLocalTranslation(new Vector3f(0, display.getHeight() - 20, 1));

      Text helpText2 = new Text("Text", "Key 'G': Switch between software/hardware mouse cursor");
      helpText2.setLocalTranslation(new Vector3f(0, 60, 1));

      currentTypeText = new Text("Text", "Current cursor type: [SOFTWARE]");
      currentTypeText.setLocalTranslation(new Vector3f(0, 40, 1));

Thanks for helping! :)

The problem is based on how input events are given by lwjgl and is not really a bug (you have events that happen outside the scope of your app.)  iirc, I solved it in my own app by writing a "focus" listener that was triggered when the display received/lost focus using isActive.

Ok, when I will do something like that. Checking if the display loses focus and if so then release the mouse buttons manually.

Thanks for the fast answer!

One more question if you dont mind… how do I check if the mouse just leaves the window?

I tried the display.isActive(), but that only checks if the window lost focus ( user has clicked on another window ). How do I check if the mouse is outside the window ( and user has not clicked on anything so the jME window still has focus)?

I'm using hardware mouse and windowed mode.

You might be able to use mouse position to do that… or you can use jme in a Canvas instead and use a mouse listener.

I've actually tried mouse position but that didn't work as expected. I found no way to get the coordinates or mouse updates when the mouse is outside the window (probably the same reason as I could not get mouse button release) so the only solution I saw was to check if it passes the border of the window. But that didn't work good because if you move the mouse quickly when it will miss the border and won't update correctly. (expanding the border region would only give other problems)

Does jme work good in a Canvas? I've read in some threads that it's buggy and for example it does not work in mac?

I works fine in a canvas.  When I was an NCsoft that is how we built our tools and they worked fine on Mac as well.