title: How to prevent user pressing multiple keys Javafx?
body: <p>I have a player that can move when pressing the arrow keys. I would like to prevent the user to press multiple arrows at the same time.
  This what I have tried:</p>
  <pre><code>boolean[] pressedKeys = new boolean[4];
  
  canvas.setOnKeyPressed(event -&gt; {
          if (!Arrays.asList(pressedKeys).contains(true)){
              if (event.getCode() == KeyCode.UP){
                      pressedKeys[0] = true;
              } else if (event.getCode() == KeyCode.RIGHT){
                      pressedKeys[1] = true;
               } else if (event.getCode() == KeyCode.DOWN){
                      pressedKeys[2] = true;
               } else if (event.getCode() == KeyCode.LEFT){
                      pressedKeys[3] = true;
               }
      }
  });
  
  canvas.setOnKeyReleased(event -&gt; {
      if (event.getCode() == KeyCode.UP){
                      pressedKeys[0] = false;
          } else if (event.getCode() == KeyCode.RIGHT){
                      pressedKeys[1] = false;
          } else if (event.getCode() == KeyCode.DOWN){
                      pressedKeys[2] = false;
          } else if (event.getCode() == KeyCode.LEFT){
                      pressedKeys[3] = false;
          }
  });
  </code></pre>
  <p>But it does not work, here I can still press the right and the up arrows for example.<br />
  Thanks for any help</p>