# keyPresssed not returning proper value

**URL:** <https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701>\
**Category:** Coding Questions\
**Created:** [February 24, 2019, 9:56pm UTC](https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701 "2019-02-24T21:56:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dragonheart0001](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@dragonheart0001](https://discourse.processing.org/u/dragonheart0001)\
**Post date:** [February 24, 2019, 9:56pm UTC](https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701/1 "2019-02-24T21:56:11Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![glitching.world](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glitching.world/32/3844_2.png) [@glitching.world](https://discourse.processing.org/u/glitching.world)\
**Post date:** [February 24, 2019, 10:05pm UTC](https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701/2 "2019-02-24T22:05:26Z")

</div>

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

```auto
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:

```auto
int startTime = 0;

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

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

```

---

<div class="post-metadata">

**Author:** ![dragonheart0001](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@dragonheart0001](https://discourse.processing.org/u/dragonheart0001)\
**Post date:** [February 25, 2019, 12:55am UTC](https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701/4 "2019-02-25T00:55:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dragonheart0001](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@dragonheart0001](https://discourse.processing.org/u/dragonheart0001)\
**Post date:** [February 25, 2019, 12:57am UTC](https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701/5 "2019-02-25T00:57:18Z")

</div>

Here’s the whole class.

```auto
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;
  }
}
      

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 25, 2019, 1:49am UTC](https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701/6 "2019-02-25T01:49:50Z")

</div>

i question your timer while concept

```auto
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:

```auto
void setup() {
}

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

```

---

<div class="post-metadata">

**Author:** ![dragonheart0001](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@dragonheart0001](https://discourse.processing.org/u/dragonheart0001)\
**Post date:** [February 25, 2019, 2:08am UTC](https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701/7 "2019-02-25T02:08:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 25, 2019, 2:12am UTC](https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701/8 "2019-02-25T02:12:10Z")

</div>

?? 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() )

---

<div class="post-metadata">

**Author:** ![dragonheart0001](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@dragonheart0001](https://discourse.processing.org/u/dragonheart0001)\
**Post date:** [February 25, 2019, 2:25am UTC](https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701/9 "2019-02-25T02:25:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 25, 2019, 2:31am UTC](https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701/10 "2019-02-25T02:31:08Z")

</div>

> [@dragonheart0001](#):
>
> 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 ?)

---

<div class="post-metadata">

**Author:** ![dragonheart0001](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@dragonheart0001](https://discourse.processing.org/u/dragonheart0001)\
**Post date:** [February 25, 2019, 2:44am UTC](https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701/11 "2019-02-25T02:44:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 25, 2019, 3:42am UTC](https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701/12 "2019-02-25T03:42:58Z")

</div>

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

```auto
// 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;
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 25, 2019, 5:24am UTC](https://discourse.processing.org/t/keypresssed-not-returning-proper-value/8701/13 "2019-02-25T05:24:05Z")

</div>

> [@dragonheart0001](#):
>
> … I’m trying to develop something like the Scanner class for Processing…

> <https://gist.github.com/GoToLoop/bba0c288aaeeb5ef9bb1>

> <https://gist.github.com/GoToLoop/8017bc38b8c8e0a5b70b>
