Detecting if the mouse is over a JME3 window?

Hi there.

At the moment I am writing an application state that handles a top-down camera and associated controls.

A part of that is that when the mouse is moved to the edge of the screen the camera is moved in the direction of the mouse. I have the border detection and mouse movement working fine.

The one problem I have is that when the program is running in a window and the cursor moved outside of the camera view the camera keeps moving in the last direction.

I’d like to simply check if the mouse is actually over the view or if it is outside of the window before I move the camera to stop the errant movement, but I cant find any means of detecting this? I’ve tried the various camera, viewport and mouse classes in use, but none seem to have any related methods?

Any help would be very much appreciated.

Thank you for reading my request. :smile:

I have been using something like that

Vector3f guiRayOrigin = new Vector3f() ;
    CollisionResult lastCollision ;
    CollisionResults results = new CollisionResults() ;
    Ray elementZOrderRay = new Ray();
        public Element getElementAtMouse() 
            guiRayOrigin.set(Mouse.getX(), Mouse.getY(), 0f);
            screen.getGUINode().collideWith(elementZOrderRay, results);
            Element testEl = null ;
                for (CollisionResult result : results) 
                    if (result.getGeometry().getParent().getUserData("boutton") != null) 
                        testEl = ((Element)(result.getGeometry().getParent()));
                        return testEl ;
                return null;

You need to make a ray like you would for a 3D object but in the 2D perpective. Then just give a commun userData to every piece of 2D interface you have and it should work. That bits of code work on toneGod, but pretty sure that whit some work it would work on Nifty. Also take note this code is made to return the element, but just change that by a boolean and you should have what you want.

Not sure its the best solution but it work pretty well on my side x)

I thank you for the attempt, but I believe there has been a misunderstanding.

What I’m looking for, is a way to detect when the mouse cursor has moved completely outside of a windowed jmonkeyengine. (for example over there desktop)

O yes indeed my bad, I miss the interpretation of window. But well, I still wonder if it would work though. I haven’t test the results of a ray getting out of the screen, but one way or a other am pretty sure you can have what you want. First, you can put the UserData on the screen, so that when it can’t find it it will throw the action that you want. And if the ray crash once out of the screen, you can put a little try catch there. Of course, if they were a pretty method in JME it would probably be better, but am not the one who knows that x)

I put together a little code to test what happens to the cursor position when the cursor leaves the window and found that it keeps being reported as the last position in the window that it was reported at (so if it leaves at 50,800, it keeps being reported as being at 50, 800 even if it has in fact moved far past that) so I don’t think that can be a part of the solution.

Thanks again for trying though.

I hacked this here in jme3.0 targeting the lwjgl2 classes; only tested on Linux (Ubuntu 14.04). Might have different behavior on Mac or Windows. InputImplementation (LWJGL API)

   private InputImplementation displayImplInstace;
   private boolean mouseInside=true;
   public void simpleInitApp() {
       try {
           Field display_impl = Display.class.getDeclaredField("display_impl");
           displayImplInstace = (InputImplementation) display_impl.get(null);
       } catch (NoSuchFieldException | IllegalAccessException e) {
    public void simpleUpdate(float tpf) {
        if(displayImplInstace.isInsideWindow() != mouseInside){

Keep in mind there might be a reason why this is not exposed in the API.

Lwjgl 3 has API do to what you want 2.6.3 Input handling with GLFW · LWJGL/lwjgl3-wiki Wiki · GitHub


That works! (on windows 7)

Thank you very much!

Its so much better when the camera doesn’t go flying through space on its own when the cursor isn’t even in the window and also reduced the movement greatly if you reach for the closed button, which is what I wanted.

My appState is still a little messy and I’m sure there is plenty of optimisation left to do, but I think everything works how I want it to now :smile: