title: Dealing with KeyEvents in java
body: <p>Say I have a GUI, and I want the program to actually run when the spacebar is pressed, but if the spacebar is pressed again then I want the program to exit. Would something like this work?</p>
  
  <pre><code>public class MouseClicker extends JApplet implements KeyListener{
  int counter = 0;
  MouseClicker m1 = new MouseClicker();
  
  //all of the other methods
  
  public void keyPressed( KeyEvent e) {
  int keyCode = e.getKeyCode();
  if(keyCode==KeyEvent.VK_SPACE){
   m1.click();
   counter ++;
   if(counter%2==0)
    System.exit(0);
  }
  //other methods needed for KeyListener
  }
  </code></pre>