title: Toggle Key in Java
body: <p>Basically I'm trying to toggle boolean every time F1 key is pressed.
  In my main class I've got update method that is called 60 times a second and I coded something like this:</p>
  
  <p>Main.class:</p>
  
  <pre><code>private Keyboard key = new Keyboard; // keyboard object
  boolean debug; // boolean to control visibility of debug screen
  private boolean toggle; // used to toggle boolean
  
  private void update() { // update is called 60/s
      if (key.isKeyPressed(Keyboard.f1) &amp;&amp; !toggle) {
          debug = !debug;
          toggle = true;
      } else if (!key.isKeyPressed(Keyboard.f1)) toggle = false;
  }
  </code></pre>
  
  <p>Keyboard.class:</p>
  
  <pre><code>public class Keyboard implements KeyListener {
  
  private boolean[] keys = new boolean[65536];
  
  public static final int f1 = KeyEvent.VK_F1; // key code of f1 key
  
  public void keyPressed(KeyEvent e) {
      keys[e.getKeyCode()] = true;
  }
  
  public void keyReleased(KeyEvent e) {
      keys[e.getKeyCode()] = false;
  }
  
  public void keyTyped(KeyEvent e) {
  }
  
  public boolean isKeyPressed(int key) {
      return keys[key];
  }
  </code></pre>
  
  <p>And this way it works perfect, but if I create similar method in Keyboard.class it does not work:</p>
  
  <p>update method in main class:</p>
  
  <pre><code>private void update() {
      key.toggle(Keyboard.f1, debug); // toggle boolean debug if f1 key pressed, but.. NOT WORKING!! WHYYY!!?
  }
  </code></pre>
  
  <p>Keyboard.class:</p>
  
  <pre><code>private boolean toggle;
  
  public void toggle(int key, boolean b) {
      if (isKeyPressed(key) &amp;&amp; !toggle) {
          b = !b;
          toggle = true;
      } else if (!isKeyPressed(key)) toggle = false;
  }
  </code></pre>
  
  <p>And my question is:
  <em>Why is the second way not working and how can I fix it to work?</em>
  Thank you!</p>