Unable to use inputs properly

Hi I’m new to jMonkeyEngine. I would like to achieve an event triggered only once during button being pressed and lifted if possible. This is because I’d like to move the “player” stepwise.

I have mostly followed the getting started example and the docs on input handling.
I am confused since by default, even though I only map WASD, other buttons and the mouse affects the window. This is highly annoying, how do I get rid of this?
(BTW I start the ClientWindow from my main method elsewhere.)

import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

public class ClientWindow extends SimpleApplication {
    private boolean isRunning = true;
    protected Geometry player;
    
    
    @Override
    public void simpleInitApp() {
        Box b = new Box(1, 1, 1);
        player = new Geometry("Player", b);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Blue);
        player.setMaterial(mat);
        rootNode.attachChild(player);
        initKeys(); // load my custom keybinding

    }

    private void initKeys() {
        // You can map one or several inputs to one named action
        //inputManager.deleteMapping( SimpleApplication.INPUT_MAPPING_MEMORY );
        inputManager.addMapping("Up",  new KeyTrigger(KeyInput.KEY_W),
                                                    new KeyTrigger(KeyInput.KEY_UP));
        inputManager.addMapping("Left",   new KeyTrigger(KeyInput.KEY_A),
                                                        new KeyTrigger(KeyInput.KEY_LEFT));
        inputManager.addMapping("Right",  new KeyTrigger(KeyInput.KEY_D),
                                                        new KeyTrigger(KeyInput.KEY_RIGHT));
        inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S),
                new KeyTrigger(KeyInput.KEY_DOWN));
        // Add the names to the action listener.
        
        inputManager.addListener(actionListener, "Up", "Left", "Right", "Down");
        // inputManager.addListener(analogListener, "Up", "Left", "Right", "Down");
    }
    
    private final ActionListener actionListener = new ActionListener() {
        @Override
        public void onAction(String name, boolean keyPressed, float tfp) {
            if(isRunning){ 
                float value=0.4f;//arbitrary offset for now.
                if (name.equals("Up") && !keyPressed) {
                    Vector3f v = player.getLocalTranslation();
                    player.setLocalTranslation(v.x, v.y + value /** speed*/, v.z);
                }
                if (name.equals("Down")&& !keyPressed) {
                    Vector3f v = player.getLocalTranslation();
                    player.setLocalTranslation(v.x, v.y - value /** speed*/, v.z);
                }
                if (name.equals("Right")&& !keyPressed) {
                    Vector3f v = player.getLocalTranslation();
                    player.setLocalTranslation(v.x - value /** speed*/, v.y, v.z);
                }
                if (name.equals("Left")&& !keyPressed) {
                    Vector3f v = player.getLocalTranslation();
                    player.setLocalTranslation(v.x + value /** speed*/, v.y, v.z);
                }
            }
        }
    };


    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
    }

}

1 Like

Do you mean that the default fly cam is still operating? “affects the window” could be interpreted a few ways.

If that’s what you mean then you can either disable the fly cam or not even include it by only including the app states that you want. Disabling the flyCam is probably easiest, though.

Thanks for the reply, despite me posting in the wrong forum.

Yes, exactly the default flyCam is still operating and I would like to disable it so that I have complete control over what happens on button presses. Also, is it possible to get rid of the display settings screen that shows whenever the window starts?

For future reference:
image

https://javadoc.jmonkeyengine.org/v3.5.2-stable/com/jme3/app/SimpleApplication.html

https://javadoc.jmonkeyengine.org/v3.5.2-stable/com/jme3/app/SimpleApplication.html#setShowSettings-boolean-

And:
image

https://javadoc.jmonkeyengine.org/v3.5.2-stable/com/jme3/input/FlyByCamera.html#setEnabled-boolean-

And note: if you don’t want to disable the fly cam you can just remove it completely by not including it… just pass the app states you want on the super constructor.

Something like:

    public ClientWindow() {
        super(new StatsAppState(), new DebugKeysAppState(), new BasicProfilerState(false),
              new ScreenshotAppState("", System.currentTimeMillis()));     
    }

The only down side is that you need to be aware of the built-in app states that are available but the above is a pretty good base set.

3 Likes