Working with input manager's mappings and listeners

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):

    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");

    protected void onDisable() {

        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


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: