[LibGDX] Using the same input key in a pause and play state

Hi,

I’m attempting to create a Pause state for my game, however its not actually a state, its just an overlay (to make it easier to resume) but the problem I’m having is when you press the escape key, it goes into the pauseState and then when you try to go back to the play state it immediately pauses again because the escape key is how you enter and exit it. I need to know how to make it so that when you press the escape key, it will only register the press once and then ignore that press in the new state.

(At the moment the game is mostly based off of ForeignGuyMike’s Block Bunny tutorial)

This is a snippet of the GameStateManager code

	private PauseState pauseState;
	public boolean paused;

	public void update(float dt) {
		gameStates.peek().update(dt);
		if(paused) {
			pauseState.update(dt);
			System.out.println("updated!");

			return;
		}
	}
	
	public void render() {
		gameStates.peek().render();
		if(paused) {
			System.out.println("paused gsm");
			pauseState.render();

			return;
		}
	}
	
	public void setPaused(boolean b) { paused = b; System.out.println("this works");}

This is the input for the pausestate code (It enters the pausestate fine, but when you press escape again it flashes back to play state and then back to the pausestate)

	public static boolean pEscape = false;
	public void handleInput() {
			if(Play.escape) {
				pEscape = false;
			}	
			if(SJInput.isPressed(SJInput.ESCAPE)) { 
				pEscape = true;
			}
			
			if(pEscape) {
				gsm.setPaused(false);
			}

	public void update(float dt) {
			handleInput();
	}

And here is the play code to enter the pauseState

	public static boolean escape = false;

			if(SJInput.isPressed(SJInput.ESCAPE)) {
				escape = true;
			}
			if(escape) {
				gsm.setPaused(true);
			}

Any ideas would be greatly appreciated. (I’ve tried taking it up with Mike, and he’s been quite helpful, but I don’t want to keep annoying him with my constant barrages of questions :P)

Thanks

Use an InputProcessor to capture input events instead of polling their state each frame.

Yeah I’m using these from the tutorial, but still with the same issue, because I’m not changing state, but putting an overlay on top.
http://pastebin.java-gaming.org/7d50a68901915

http://pastebin.java-gaming.org/d50a870991518

If game is paused don’t update game states, only paused state.

1 - You can use Gdx.input.isKeyJustPressed.

switch (gameState)
{
	case isPlaying:
		if (Gdx.input.isKeyJustPressed(Keys.ESCAPE))
			gameState = isPaused;
	break;
	case isPaused:
		if (Gdx.input.isKeyJustPressed(Keys.ESCAPE))
			gameState = isPlaying;
	break;
}

2 - You can use a custom InputProcessor to set keydown/keyup:

GameInputProcessor

public class GameInputProcessor extends InputAdapter
{
	public boolean keyDown (int k)
	{
		if (k == Keys.ESCAPE)
			GameKeys.setKey (GameKeys.ESCAPE, true);
		return true;
	}

	public boolean keyUp (int k)
	{
		if (k == Keys.ESCAPE)
			GameKeys.setKey (GameKeys.ESCAPE, false);
		return true;
	}
}

GameKeys

public class GameKeys
{
	private static boolean[] keys;
	private static boolean[] pKeys;
	private static final int numKeys = 1;
	public static final int ESCAPE = 0;
	
	static
	{
		keys = new boolean[numKeys];
		pKeys = new boolean[numKeys];
	}
	
	public static void update()
	{
		for (int i=0; i< numKeys; i++)
		{
				pKeys[i] = keys[i];
		}
	}
	
	public static void setKey (int k, boolean b)
	{
		keys[k] = b;
	}
	
	public static boolean isDown (int k)
	{
		return keys[k];
	}
	
	public static boolean isPressed(int k)
	{
		return keys[k] && !pKeys[k];
	}
}

Create

Gdx.input.setInputProcessor(new GameInputProcessor());

Render

if (GameKeys.isDown(GameKeys.ESCAPE))
	//some code