title: How to pause/play a sketch in processing with the same button?
body: <p>I'm trying to implement a pause/play for a sketch with the same key, for example if I press p the sketch should stop and if I press p again, the sketch should start again. So far I used the noLoop()/loop() to do this but with two different keys (p for pause, r for start). It does work if I use keyPressed() and keyReleased() but this means to hold down the key but this doesn't answer my question. Also in the pause mode I used redraw() for a single step while noLoop() and works good.
  Here is some code I tried so far with two different keys:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>public void draw(){
      background(random(255));
  }
  
  public void keyPressed(){
      if ( key == 'p' )
          noLoop();
      if ( key == 'r' )
          loop();
      if ( key == 's' )
          redraw();
  }
  </code></pre>
  
  <p>And this is the code with the same key:</p>
  
  <pre class="lang-java prettyprint-override"><code>public void draw(){
      background(random(255));
  }
  
  public void keyPressed(){
      if ( key == 'p' )
          noLoop();
      if ( key == 'p' )
          loop();
      if ( key == 's' )
          redraw();
  }
  </code></pre>
  
  <p>In this case when I press key it doesn't have any effect.
  And the last one I tried is this:</p>
  
  <pre class="lang-java prettyprint-override"><code>public void draw(){
      background(random(255));
  }
  public void keyPressed(){
      if ( key == 'p' )
          noLoop();
      else
          loop();
      if ( key == 's' )
          redraw();
  }
  </code></pre>
  
  <p>In this case when I press 'p' it stops the sketch but is doesn't play again. Because of the 'else' it plays again when I press any key including 's' which suppose to be just for a single step.
  Any help is more than welcome.
  Thanks!</p>