# If statement with keyboard keyPressed()

**URL:** <https://discourse.processing.org/t/if-statement-with-keyboard-keypressed/41226>\
**Category:** Coding Questions\
**Created:** [March 10, 2023, 6:39pm UTC](https://discourse.processing.org/t/if-statement-with-keyboard-keypressed/41226 "2023-03-10T18:39:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@Dave](https://discourse.processing.org/u/Dave)\
**Post date:** [March 10, 2023, 6:39pm UTC](https://discourse.processing.org/t/if-statement-with-keyboard-keypressed/41226/1 "2023-03-10T18:39:34Z")

</div>

I have included the code just to make it easier to explain what i am trying to do as i am now thinking scrap most of this code and do it a different way so if someone could point me in the right direction i would be grateful . So i am now thinking to get a letter and number input from arduino such as “ght19.5” (That bit i am ok with) and the processing code will interpret this as --Greenhouse temp is 19.5-- and move the pointer accordingly . if your wondering not using serial out from arduino because i am hopping to add lot of sensors and i think it would get messy  
Thankyou for your time

```auto
`//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Using to monitor lots of different sensors around garden and green house //
// fed in to arduinos with keyboard emulators //
// Keyboard inputs gt1,gt2,gt3---to--gt9,gtt1----to--gtt for temp //
// //
// //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

String t = ""; //Keyboard input
String word; // To match keyboard input
int press = 0; // if input no input match within amount of presses
int l = 0; // pointer position
PShape s; // The PShape object (pointer)
int y = 180; // to move Pointer
int rectWidth;
int x ; //box position
int coverx = 0 ; // x position box to cover old pointer
int covery = 0 ; // y position box to cover old pointer

void setup()
{
  size(640, 360, P3D); // full screen
  noStroke(); // no outline
  background(0); // Background black
  rectWidth = width/4; // ??
  /////////////////////////////////////// Pointer ///////////////////
  s = createShape(); //
  s.beginShape();
  s.fill(255, 255, 255);
  s.noStroke();
  s.vertex(0, 0);
  // s.vertex(0, 0);
  s.vertex(20, 20);
  s.vertex(40, 0);
  s.endShape(CLOSE);
}
/////////////////////////////////////////////////////////////////////////////////////
void draw()
{
  textSize(70);
  fill(600, 600, 612);

  line(200, 200); //temp line
  lineh(200, 300); //humid line
  linec(200, 400); // co2 line
}

////////////////////////////////////////////////////////////////////////////////
void keyPressed()
{

  press++ ; // count to check for wrong input
  coverx = 170; //x position box to cover old pointer
  covery = 165; //y position box to cover old pointer

  println(press);//debug
   t = t + key;

  if (press < 5 ) {

    l = 170 ;
    if (t.equals("gt1")) //keyboard input

    {
      word = t; // store word

      t= ""; //clear t

      y = 180; // move pointer
      fill(0);
      rect(coverx, covery, 300, 30); //cover old pointer
      shape(s, y, l); //pointer
      press = 0 ; //court presses
    }

    if (t.equals("gt2")) //keyboard input
    {
      word = t; // store word
      t= ""; //clear t
      fill(0); //box colour
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 200; // move pointer
      shape(s, y, l); //pointer
      press = 0 ;
    }

    if (t.equals("gt3")) //keyboard input
    {
      word = t; // store word
      t = ""; //clear t
      fill(0); //pointer color
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 220; // move pointer
      shape(s, y, l);//pointer
      press = 0 ;
    }

    if (t.equals("gt4")) //keyboard input
    {
      word = t; // store word
      t = ""; //clear t
      fill(0); //box colour
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 240; // move pointer
      shape(s, y, l); //pointer
      press = 0 ; // press counter
    }
    if (t.equals("gt5")) //keyboard input
    {
      word = t; // store word
      t= ""; //clear t
      fill(0); //box colour
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 260; // move pointer
      shape(s, y, l); //pointer
      press = 0 ;
    }

    if (t.equals("gt6")) //keyboard input
    {
      word = t; // store word
      t = ""; //clear t
      fill(0); //box colour
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 280; // move pointer
      shape(s, y, l); //pointer
      press = 0 ; //press counter
    }

    if (t.equals("gt7")) //keyboard input
    {
      word = t; // store word
      t = ""; //clear t
      fill(0); //pointer colour
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 300; // move pointer
      shape(s, y, l); //pointer
      press = 0 ; //press counter
    }

    if (t.equals("gt8")) //keyboard input
    {
      word = t; // store word
      t = ""; //clear t
      fill(0); //box colour
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 320; // move pointer
      shape(s, y, l); //pointer
      press = 0 ; //press counter
    }

    if (t.equals("gt9")) //keyboard input
    {
      word = t; // store word
      t = ""; //clear t
      fill(0); //box colour
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 340; // move pointer
      shape(s, y, l); //pointer
      press = 0 ; //press counter
    }

    if (t.equals("gtt1")) //keyboard input
    {
      word = t; // store word
      t = ""; //clear t
      fill(0); //box colour
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 350; // move pointer
      shape(s, y, l); //pointer
      press = 0 ; //press counter
    }

    if (t.equals("gtt2")) //keyboard input
    {
      word = t; // store word
      t = ""; //clear t
      fill(0); //box colour
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 360; // move pointer
      shape(s, y, l); //pointer
      press = 0 ; // press counter
    }

    if (t.equals("gtt3")) //keyboard input
    {
      word = t; // store word
      t = ""; //clear t
      fill(0); //box colour
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 380; // move pointer
      shape(s, y, l); //pointer
      press = 0 ; //press counter
    }

    if (t.equals("gtt4")) //keyboard input
    {
      word = t; // store word
      t = ""; //clear t
      fill(0); //box colour
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 400; // move pointer
      shape(s, y, l); //pointer
      press = 0 ; //press counter
    }

    if (t.equals("gtt5")) //keyboard input
    {
      word = t; // store word
      t = ""; //clear t
      fill(0); //pointer colour
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 420; // move pointer
      shape(s, y, l); //pointer
      press = 0 ; //press counter
    }

    if (t.equals("gtt6")) //keyboard input
    {
      word = t; // store word
      t = ""; //clear t
      fill(0); //box colour
      rect(coverx, covery, 300, 30); //cover old pointer
      y = 440; // move pointer
      shape(s, y, l); //pointer
      press = 0 ; //press counter
    }
  } else if (press > 4 ) { // clear string if z is pressed

    if (t.equals("z")) //keyboard input
    {
      word = t; // store word
      println(word); //debug

      t= ""; //clear t
      println(word); //debug
      press = 0 ; //press counter
    }

    t= ""; //press z to clear string
  }
}
 void line (int x,int y)// blue green red //// cold ok hot
  {
  textSize(35);
  fill(600, 600, 612);
  text("Temp",x - 110,y+25);
  fill(0, 0, 600);
  rect(x, y, 75, 30); //move accross,move up,size accross.size up
  fill(0, 600, 0);
  rect(x+80, y, 100, 30); 
  fill(600, 0, 0);
  rect(x+183, y, 75, 30); 

  textSize(20);
  fill(0);
  text("Cold", x +20, y +20);
// fill(255);
  text("Good", x+110,y+ 20);
 // fill(0, 0, 0);
  text("Hot", x + 200, y+20);
  fill(600, 600, 612);
    
  

}

 void lineh (int x,int y)// co2
  {
  textSize(35);
  fill(600, 600, 612);
  text("Humidity",x-150 ,y+25);
  fill(0, 0, 600);
  rect(x, y, 75, 30); //move accross,move up,size accross.size up
  fill(0, 600, 0);
  rect(x+80, y, 100, 30); 
  fill(600, 0, 0);
  rect(x+183, y, 75, 30); 

  textSize(20);
  fill(0);
  text("Wet", x +20, y +20);
// fill(255);
  text("Good", x+110,y+ 20);
 // fill(0, 0, 0);
  text("Dry", x + 210, y+20);
  fill(600, 600, 612);
    }
    
    void linec (int x,int y)// co2
  {
  textSize(35);
  fill(600, 600, 612);
  text("C02",x - 110,y+25);
  fill(0, 0, 600);
  rect(x, y, 75, 30); //move accross,move up,size accross.size up
  fill(0, 600, 0);
  rect(x+80, y, 100, 30); 
  fill(600, 0, 0);
  rect(x+183, y, 75, 30); 

  textSize(20);
  fill(0);
  text("low", x +20, y +20);
// fill(255);
  text("Good", x+110,y+ 20);
 // fill(0, 0, 0);
  text("Danger", x + 190, y+20);
  fill(600, 600, 612);
    }
    

```

```
`<!-- 

```

Please format your code using the \</\> button  
More on code formatting: [Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147#is-your-post-formatted-9)  
Homework policy: [https://discourse.processing.org/t/faq-guidelines/5#homework](https://discourse.processing.org/t/faq-guidelines/5#homework)  
Asking Questions: [Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147)  
 →

---

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [March 10, 2023, 10:54pm UTC](https://discourse.processing.org/t/if-statement-with-keyboard-keypressed/41226/2 "2023-03-10T22:54:32Z")

</div>

Do you mean something like that?

```auto
void keyPressed() {
   text = text+key;
} // void keyPressed stops after executed once, to execute it again, press again!

```

Here is a very basic text editor:

```auto
String text = ""; //needs to be "" bc it cant be null
void setup() {
  size(960,540);
  textSize(14);
}
void draw() {
  background(25);
  text(text,10,20);
}
void keyPressed() {
  if(!(key == '')) text = text + key; //adds a character
  if(key == '') text = text.substring( 0, text.length()-1 ); //removes one
  println(key); //unncessary :3
}

```
