Key Pressed to Change text

Hello! I am trying to add text with every keypressed. So if key is pressed, words “I BET” show up, then when key pressed again, “YOU DIDN’T”… and so on.

So far this is what I have: it shows all the text at once when key is pressed. Anyone out there know how to help? Thanks:)

void setup() {
  size (1000,1000);
  background (0,0,255);
}

void draw() { } // Empty draw() needed to keep the program running

void keyPressed() {
  if (keyPressed == true) {
    textSize (100);
    fill(255);
    text ("I BET", 0, 100);//("pressed " + int(key) + " " + keyCode);
  } else {
    fill(255);
  }
  if (keyPressed == true) {
    textSize (100);
    fill(255);
    text ("YOU DIDN'T", 0, 200);//("pressed " + int(key) + " " + keyCode);
  } else {
    fill(255);  
  }
  if (keyPressed == true) {
    textSize (100);
    fill(255);
    text ("THINK", 0, 300);//("pressed " + int(key) + " " + keyCode);
  } else {
    fill(255);
  }
  if (keyPressed == true) {
    textSize (100);
    fill(255);
    text ("YOU LOOKED", 0, 400);//("pressed " + int(key) + " " + keyCode);
  } else {
    fill(255);
  }
  if (keyPressed == true) {
    textSize (100);
    fill(255);
    text ("THIS GOOD", 0, 500);//("pressed " + int(key) + " " + keyCode);
  } else {
    fill(255);
  }
  if (keyPressed == true) {
    textSize (100);
    fill(255);
    text ("WHILE WORKING", 0, 600);//("pressed " + int(key) + " " + keyCode);
  } else {
    fill(255);
  }
  if (keyPressed == true) {
    textSize (100);
    fill(255);
    text ("OUT", 0, 700);//("pressed " + int(key) + " " + keyCode);
  } else {
    fill(255);
  }
}
1 Like

Hello and welcome to the processing forum!

void keyPressed() {is called when a key is pressed only; so no need to check if (keyPressed == true) { inside of it.

But remember, that processing runs through the entire keyPressed() so all texts are shown.

What you could do is that you have a int variable textNumber and increase it every time a key is pressed. Now, in draw you check textNumber :

if(textNumber == 1) {
....
} else if(textNumber == 2) {
....
} 
.....

Warm regards, Chrisir

1 Like

Hey aranzz:
This seemed like fun, so pulled up a copy of my empty, log enabled shell sketch
and popped in an expression of Chrisr’s suggestion.

import org.apache.log4j.*;
/* Thank you Jake Seigel for the processing log4j connectivity!
 * https://jestermax.wordpress.com/2014/06/09/log4j-4-you/ 
 *
 * I'm a logggerDebugger and this was key to being able to 
 * dig myself out of several/many  holes/traps/mistakes/typos:  
 */
Logger log  = Logger.getLogger("Master"); /* Thank you Jake Seigel   https://jestermax.wordpress.com/2014/06/09/log4j-4-you/     */
Logger log1 = Logger.getLogger("Ancillary0"); /* Thank you Jake Seigel   https://jestermax.wordpress.com/2014/06/09/log4j-4-you/   */
PFont font;

String[] sayings = {"I BET", "YOU DIDN'T", "THINK", "YOU LOOKED", "THIS GOOD", "WHILE WORKING", "OUT!"};
int index=0;
/*********************************************************************************************************************************/
/*********************************************************************************************************************************/
/*********************************************************************************************************************************/
void setup(){
  size(640,480);
  smooth(8);
  initLog4j();
  colorMode(RGB, 1);
  font = createFont("Arial.bold", 60); 
  textFont(font);
}
/*********************************************************************************************************************************/
void draw(){
  background(0.,0.,0.5); /* need to clear the previous screen */
  //fill(0.,0.,1.);
  //text("Hello World",10,260); 
  
  /* could have used 7 instead of sayings.length for the modulo (%) operator, 
   * but this will be a start at more re-useable code
   */
  text(sayings[index % sayings.length],10,260); 
}
/*********************************************************************************************************************************/
void keyPressed() {
  index++;
} 
/*********************************************************************************************************************************/
void initLog4j(){
  /* Log stuff follows the input from Jake Seigel:   https://jestermax.wordpress.com/2014/06/09/log4j-4-you/ */
  
  /* Tried out several message patterns till I found one I liked   */
  //String masterPattern = "[%c{1}], %d{HH:mm:ss}, %-5p, {%C}, %m%n";
  //String masterPattern = "%-5p %8r %3L %c{1} - %m%n";  
  //String masterPattern = "%-5p - %m%n"; /* source - message */
  String masterPattern = "%-5p %8r - %m%n"; /* source miliseconds - message *?
  
  /* the following line is instanced in the header for the main processing app, so as to have app wide scope */
  //Logger log  = Logger.getLogger("Master"); /* Thank you Jake Seigel   https://jestermax.wordpress.com/2014/06/09/log4j-4-you/   */
  FileAppender fa0 = new FileAppender();
  String fileName="logger.log";
  fa0.setFile(System.getProperty("os.name").toLowerCase().startsWith("win")?"c:\\logs\\"+fileName:sketchPath("/home/pi/logs/"+fileName));  
  fa0.setLayout(new PatternLayout(masterPattern));
  fa0.setThreshold(Level.DEBUG);
  fa0.setAppend(false);
  fa0.activateOptions();  
  //Logger.getRootLogger().addAppender(fa0); /* this causes all loggers to be collected into the file pointed to by fa0 */
  log.addAppender(fa0); /* this causes the fa0 file to get only the content sent to log */
  
  log.trace("log checking ability to write at trace level"); /* not believed to be part of log4j, maby it came in with log4j2???  */
  log.debug("log checking ability to write at debug level");
  log.info ("log checking ability to write at info  level");
  log.warn ("log checking ability to write at warn  level");
  log.error("log checking ability to write at error level");
  log.fatal("log checking ability to write at fatal level\nIf you see this in the Eclipse console, your log4j2.xml was not active (it should be in /src/main/resources)\n");
  try{
    log.fatal("this log file was created by "+System.getProperty("sun.java.command").substring(System.getProperty("sun.java.command").indexOf("path=")+5));
   } catch (Exception e) {
    e.printStackTrace();
  }
  String ancillaryPattern="%m%n"; /* message only */
  /* the following line is instanced in the header for the main processing app, so as to have app wide scope */ 
  /*Logger log1 = Logger.getLogger("Ancillary0"); /* Thank you Jake Seigel   https://jestermax.wordpress.com/2014/06/09/log4j-4-you/   */
  FileAppender fa1 = new FileAppender();
  fa1.setFile(System.getProperty("os.name").toLowerCase().startsWith("win")?"c:\\logs\\logger1.csv":sketchPath("/home/pi/logs/logger1.csv")); 
  fa1.setLayout(new PatternLayout(ancillaryPattern));
  fa1.setThreshold(Level.DEBUG);
  fa1.setAppend(false);
  fa1.activateOptions();
  log1.addAppender(fa1);
  
  //log1.trace("log1 checking ability to write at trace level"); /* not believed to be part of log4j, maby it came in with log4j2???  */
  //log1.debug("log1 checking ability to write at debug level");
  //log1.info ("log1 checking ability to write at info  level");
  //log1.warn ("log1 checking ability to write at warn  level");
  //log1.error("log1 checking ability to write at error level");
  //log1.fatal("log1 checking ability to write at fatal level\nIf you see this in the Eclipse console, your log4j2.xml was not active (it should be in /src/main/resources)");  
}
/*********************************************************************************************************************************/
1 Like