Search code examples
javakeypresskeyevent

Adjust Key Pressed So You Cant "Hold" it


Wasn't sure how to word the title.

I have some code for my "space invaders" type game. I made the entire thing just making adjustments. To shoot, i use spacebar. The problem is im able to hold spacebar and it continually shoots. I would rather have to press it multiple times (if i hold it down.. for it not to continually fire) How would i change it?

*here is the code i believe is the source of the problem. If the source is located elsewhere please say so.

private class KeyInputHandler extends KeyAdapter {
    private int pressCount = 1;

    /**
     * key pressed
     */
    public void keyPressed(KeyEvent e) {
        if (waitingForKeyPress) {
            return;
        }

        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            leftPressed = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            rightPressed = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            firePressed = true;
        }
    } 

    /**
     * Key Released
     */
    public void keyReleased(KeyEvent e) {

        if (waitingForKeyPress) {
            return;
        }

        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            leftPressed = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            rightPressed = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            firePressed = false;
        }
    }

added code

public class ShotEntity extends Entity {
    //vertical speed
    private double moveSpeed = -300;
private Game game;

private boolean used = false;

/**
 * Create a new shot from the player
*/
public ShotEntity(Game game,String sprite,int x,int y) {
    super(sprite,x,y);

    this.game = game;

    dy = moveSpeed;
}

/**
 * Request that this shot moved based on time
 */
public void move(long delta) {
    super.move(delta);

    if (y < -100) {
        game.removeEntity(this);
    }
}

//collision
public void collidedWith(Entity other) {

    // prevents double kills.
    if (used) {
        return;
    }

    // alien killed
    if (other instanceof AlienEntity) {
        game.removeEntity(this);
        game.removeEntity(other);

        game.notifyAlienKilled();
        used = true;
    }
}

}


Solution

  • Listen for two seperate key events. One when the key is pressed, and one when it is released. Now, you make a boolean. public boolean myBoolean; When the key is pressed, set the boolean to true (myBoolean==true)and if it is released, set it to false (myBoolean==false).

    Now, create a simple if statement:

    if(myBoolean==true){ //Key is being held down
    //execute this code
    //Have nothing be done
    }
    

    Thats it!

    P.S. If you want, you can have one separate method to clean up the code. This method could just change the value of the boolean:

    public void changeValueOfBoolean(Boolean b){
    if(b = true){
    return false;
    }
    if(b==false){
    return true;
    }
    }
    

    It's very simple. Let me know if you need any help, and if this was helpful, please mark it as best answer. Feel free to ask me any questions, I am always happy to help!

    Clarifications:

    Okay, so we have this boolean, right? Now, this boolean essencialy decides whether the key is pressed or not.

    Say we press the button:

    Because the key is being held down, we make the boolean myBoolean as true. Now, because the user is holding the key down, we want to do nothing, right? That's why we created that if statement:

     if(myBoolean==true){ //Key is being held down
        //execute this code
        //Have nothing be done
        }
    

    It says, if the boolean is true, then we won't do anything. Recall, the boolean is true when the key is being held down, remember? So, when the key is held down, the boolean is true, and nothing will be done.

    Now, when the user releases the key:

    Now, say the user has pressed the key. This will call two listeners. One listener for when the key is pressed and another for when it is released. If the user just presses the key, myBoolean will end as false, since they have let go of the key. Therefore, we can execute some code when myBoolean is false (shooting).

    **In summary:

    myBoolean is true when when the key is being held down. So, when it is true, we do nothing. myBoolean is false when the user lets go of the key, and there, we shoot the space invaders.**