keyPresssed not returning proper value

I am trying to create a basic input interface for project I am working on and am running into a bug where the keyPressed variable always returns false. Here’s the basic idea of the code.

void setup() {
  int startTime = millis();
  while (startTime + 60000 > millis()) {
    println(keyPressed);
  }
}
1 Like

I think this way should be working. I’m a beginner so I’m not sure how to explain what I did :rofl: Be sure to focus the window to see it work.

int startTime = 0;

void setup() {
  startTime = millis();
}

void draw() {
  if (startTime + 60000 > millis()) {
    println(keyPressed);
  }
}

This will only return true or false values.
But this might be more helpful:

int startTime = 0;

void setup() {
  startTime = millis();
}

void draw() {
  if (startTime + 60000 > millis()) {
    if (keyPressed == true) {
      println(key);
    }
  }
}
1 Like

That works; however, for what I need to do, I need this to work in the setup method. I have a method like this in a class which I then call in another class’s constructor. I would just use the Scanner class, but Processing does not have native console support.

Here’s the whole class.

class Input {
  private String text;
  private StringBuilder input;
  private char current;
  
  Input() {
    text = "";
    input = new StringBuilder(text);
  }
  
  public String type() {
    int startTime = millis();
    while (startTime + 60000 > millis()) {
      println(keyPressed);
      if (keyPressed) {
        if (key == BACKSPACE) {
          text.replace(current, ' ');
          println(text);
        }else if(key == RETURN || key == ENTER){
          break;
        }else {
          current = key;
          input.insert(text.length(), current);
          //text = input.toString();
          println(current);
        }
      }
    }
    return text;
  }
}
      

i question your timer while concept

int startTime = millis(); 
while (startTime + 60000 > millis()) { 
}

-a- for keyboard input you need to run DRAW
( so from setup it will never work )

-b- and a while(timer) is like a delay, not do that in draw.

play with that code to understand that it is NOT working:

void setup() {
}

void draw() {
  int startTime = millis();
  println("start");
  while (startTime + 10000 > millis()) {
    if ( keyPressed) println(key);
  }
  println("stop");
}

Thank you. The loop using the timer is simply because Processing does not allow infinite loops with a break statement within. I could possibly change it to check a boolean and update that boolean when the needed condition is met.

?? did you try that code ( what is a made from your class )
did you see that it does not see the keyboard
in side that while loop ( not even running inside draw() )

Oh. Yes. I realized that after I posted my reply. It also doesn’t even work when I change the while statement to a different conditional. I am probably going to give up on trying to incorporate an interface into my project; it isn’t necessary, and it seems Processing really makes it difficult to develop anything like that.

now that is a point where the experts might help you with,
if you able to describe what you try to do???
( nothing about class and setup … , the pure function you need ?)

Essentially, I’m trying to develop something like the Scanner class for Processing since Processing doesn’t support native console input. This is in order to set variables within another class.

ok, we not talk same language,
but why you not just start about a normal processing keyboard string input?

// keyboard string input

String last="", input="";
boolean userinput;

void setup() {
  size(200, 200);
  fill(0, 0, 200);
}

void draw() {
  if (userinput ) { 
    background(200, 200, 0);
    text("last: "+last, 10, height-40);
  } else            background(0, 200, 200);
  text("type: "+input, 10, height-20);
}

void keyPressed() {
  println("key "+key+" keyCode "+keyCode);
  if ( key == ENTER || key == RETURN ) { 
    last = input;                               // remember that "line"
    input = "";                                 // clean for next "line"
    userinput = true;                           // set a global for further processing
  } else if ( key == TAB || key == ESC ) ;      // ignore
  else if ( ( key == DELETE || key == BACKSPACE ) && input.length() > 0 ) 
    input = input.substring(0, input.length()-1);
  else if ( key != CODED )   input += key;
}

1 Like