Simple keyboard toggle switch?

Hey everyone. I'm trying to create a simple keyboard toggle switch. When a key is pressed, the application should pause. When the same key is pressed again, the application should resume. Here's what I have so far. Many thanks for the help.

boolean pause = false;

void keyPressed() {
  if ((key == 'x' || key == 'X') && (pause = false)) pause = true; noLoop();
  if ((key == 'x' || key == 'X') && (pause = true)) pause = false; loop();
}

Answers

  • edited October 2015 Answer ✓
    boolean isPaused;
    
    void draw() {
      background((color) random(#000000));
    }
    
    void keyPressed() {
      if (isPaused ^= keyCode == 'X')  noLoop();
      else                             loop();
    }
    
  • Wonderful. Thank you!

Sign In or Register to comment.