Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a keyListener go back to previous key
    text
    copied!<p>so I have a keyListener, I finally overcame the Keyboard input delay. I now have a new problem, <strong>When a user presses the "UP" arrow key to move "UP" they move "UP" for however long they want, then when the user presses the "DOWN" Arrow key, (<em>while</em> still holding the "UP" Arrow key) they move "DOWN".</strong> </p> <p><strong>How do I allow them to move "UP" again after the "DOWN" Arrow key has been released, without having to press the "UP" key again?</strong></p> <p><strong>(NOTE: The "UP" key is pressed throughout the whole process.)</strong></p> <p>here is the code I have currently use for listening for input. </p> <pre><code>private void keyListeners(){ GlobalObjects.frame.addKeyListener(new KeyListener() { @Override public void keyPressed(KeyEvent e) { //Up = 1 //Down = 2 //Left = 3 //Right = 4 if(e.getKeyCode() == 38){ System.out.println("UP KEY PRESSED"); player.setPlayerMovement(1); }else if(e.getKeyCode() == 40){ System.out.println("DOWN KEY PRESSED"); player.setPlayerMovement(2); }else if(e.getKeyCode() == 37){ player.setPlayerMovement(3); }else if(e.getKeyCode() == 39){ player.setPlayerMovement(4); } } @Override public void keyReleased(KeyEvent e) { if(e.getKeyCode() == 38){ if(player.getPlayerMovement() != 1){ System.out.println("UP KEY released, but some other arrow key changed the playermovement to a different number (do nothing)"); }else{ System.out.println("UP Key was a the last key pressed, therefore its safe to set player movement to "0" (not moving)"); player.setPlayerMovement(0); } }else if(e.getKeyCode() == 40){ if(player.getPlayerMovement() != 2){ }else{ player.setPlayerMovement(0); } }else if(e.getKeyCode() == 37){ if(player.getPlayerMovement() != 3){ }else{ player.setPlayerMovement(0); } }else if(e.getKeyCode() == 39){ if(player.getPlayerMovement() != 4){ }else{ player.setPlayerMovement(0); } } } @Override public void keyTyped(KeyEvent e) { } }); } </code></pre> <p>If you couldn't tell, this is a 2D RPG based game. (if that helps at all, answering the question)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload