Working with input manager's mappings and listeners

Hello,
Now I want to know the best way for working with mappings and listeners, that I sets up in my different app states.

I want to use different key triggers in each app state. But when I’m adding mappings and listeners in different app states I’m doing this for one input manager. As I understand, regardless of whether app state enabled or disabled mappings and listeners that I set up in this app state for inputManager will work, if I set up this mappings and listeners in it’s initialize method(mappings and listeners will work after I will initialize app state).

But I want to have different mappings and listeners in different app states.
For this now I adds mappings and adds listeners for each of this mappings in app state’s onEnable method and deletes all mappings and removes listeners for them in onDisable method.

Do I do it best way? The way I described you is the most common and most people doing it like me or not?

Also I would like to know, is it right, that when I adds listener - I do this for each mapping but when I removes listener I do this once? I mean now I have next structure of adding and removing mappings and listeners(it will help you also for answering to previous question):

...
    @Override
    protected void onEnable() {
        inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
        inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));

        inputManager.addListener(actionListener, "Left");
        inputManager.addListener(actionListener, "Right");
        inputManager.addListener(actionListener, "Up");
        inputManager.addListener(actionListener, "Down");
    }

    @Override
    protected void onDisable() {
        inputManager.deleteMapping("Left");
        inputManager.deleteMapping("Right");
        inputManager.deleteMapping("Up");
        inputManager.deleteMapping("Down");

        inputManager.removeListener(actionListener);  // removing it once
    }
...

I don’t have to remove listener for each mapping or something like this? I think so, but got little cognitive dissonance when added listener for every mapping and removed it once :slight_smile:

1 Like

You can also use

com.simsilica.lemur.input.InputMapper;

as in here InputMapper and AppState Removal
or create something similar like that to ease up the process.

InputMapper is nice because you can disable/enable whole groups of mappings at once after they are already setup.

It’s also way easier to use (in my biased opinion) and supports key combos.

Thank you all. I’ll try InputMapper as you adviced

This might be helpful: