How'd you implement an action not being repeated when the button is held down?

I’m pretty sure I can find my own way around this, but I was curious to learn how you folks do it/would do it.

Like for example:

(1) I press the jump button
(2) My character jumps
(3) I just hold the jump button down after pressing it
(4) My character falls to the ground with me still holding the jump button

If you don’t want the character to jump again if the player is still holding down the button that made the character jump, how would you do it?

Same thing like shooting for example, how to you prevent continuous shooting when the shoot button is held and instead require the player to press the button repeatedly instead?

It depends! Check if the library you have provides the functionality you need. Some of them do follow when a key was first pressed. That’s what you’re looking for.

If it doesn’t, then there are several ways to do it. If it’s an event driven system you’ll get an event that says “Key was pressed” which you can react to. (The merits of using event driven input in games is another discussion. :D)

If it’s a polling system (You ask whether it’s up or down and it tells you) then you have your own key object that would keep track of the button’s state. The object would have: isButtonDown() and isFirstDown(), or something like that. The first one returns true if the button is currently down. The second will only return true during the time step in which it first went from being up to being down. How you do this internally is up to you. Since you’ll typically be polling the keys EVERY time step, I’d do something like the code that follows. Yes, you end up setting firstDown a BUNCH of times when you might not need to, but meh.


class GameButton {
	// Members used to keep track of key information for KEY IS PRESSED check
	private boolean down;
	private boolean firstDown;
	public void pollButton() {
		if( KEY IS PRESSED) {
			if(!down) {
				down = true;
				firstDown = true;
			} else {
				firstDown = false;
			}
		} else {
			down = false;
		}
	}
	
	public boolean isDown() {
		return down;
	}
	
	public boolean isFirstDown() {
		return firstDown;
	}
}

You could have a boolean. jumpReady.
That’s set to true when key is released and you are on the ground and false when you are in the air and key is pressed.

Depends on what game you are doing though.

You don’t mention what technologies and frameworks you are using, but not too worry the approach is generally the same.

Basically you are talking about game state, i.e. whether the character is jumping or not in your scenario.

Presumably you have some sort of callback handler that starts the jump when you press the button, and possible some sort of listener that knows when the jump animation has completed? You’ll need to check whether you’re already jumping, plus you need some means of letting it know when the jump has ended, i.e. something along these lines (in pseudo code):

handler() {
  if( !jumping ) {
    start jump animation // calls animation-listener() when it's finished
    jumping = true;
  }
  // otherwise ignore keypress
}

animation-listener() {
  jumping = false;
}

The jumping boolean is the game state.

The above is very crude but hopefully you get the idea.

Sorry, I’m using JInput, but I’m interested in the different approaches nonetheless :slight_smile: Thanks.

Maybe these can help?


public class MyListener implements java.awt.event.KeyListener
{
	//...

	@Override
	public void keyPressed( KeyEvent keyEvent )
	{
		// What to do when the key is pressed
	}

	@Override
	public void keyReleased( KeyEvent keyEvent )
	{
		// What to do when the key is released
	}

	@Override
	public void keyTyped( KeyEvent keyEvent )
	{
		// I'm still not clear on the point of this one
	}

	//...
}


In my code, I have the character start moving upon a keydown event, and stop moving on a keyup.

From what I’ve seen, more advanced input managers also have a timer indicating how long the key has been pressed, or how long since it was pressed.

Poll your input and store it in a separate boolean or int array then do som xor magic.

I stored the pressed key’s in a array.
Then I created a method to release the key from the array.

So there is a simple boolean check to see if a key is down (in the array).

So check if your key is down, if it is do the action, after the action, release the key.