how to check if the textfield is active controlp5

edited April 2017 in Library Questions

Hello. Basically I'm getting outputs from key pressed buttons and I also want to have textfields. How do I make sure that when I'm writing on a textfield I don't get the outputs from the main code. I started with the code like this and I think the problem is I don't know how the function isFocus() works. Onoff is never getting true

import controlP5.*;
boolean onoff = false;
ControlP5 cp5;

void keyPressed() {
  }
  if ((key == 'a') && (onoff == false)) {
    //Do something normally
  }
}

cp5 = new ControlP5(this);
cp5.addTextfield("input_1")
.setPosition(20, 40)
.setSize(200, 40)

Textfield tf = (Textfield) cp5.getController("input_1");
    if (tf.isFocus() == true) {
      onoff=true;
    } 
    else {
      onoff=false;
    }
Tagged:

Answers

  • Answer ✓

    Try this code. You are missing the draw and setup functions which are needed for controlP5 to be able to have that iteractive behavior that you are looking for. Check this link for more info: https://processing.org/reference/draw_.html

    Kf

    import controlP5.*;
    
    ControlP5 cp5;
    Textfield tf;
    
    String mytext;
    
    void setup() {
      size(400,400);
      textAlign(CENTER,CENTER);
      fill(0);
      mytext="";
    
      cp5 = new ControlP5(this);
      tf=cp5.addTextfield("input_1")
        .setPosition(20, 40)
        .setSize(200, 40);
    
      //Textfield tf = (Textfield) cp5.getController("input_1");
    }
    
    void draw() {
      background(255);
    
      println(""+tf.isFocus());
      text(mytext,width/2,height/2);
    
    }
    
    void keyPressed() {
    
      if (tf.isFocus() == false) {
        mytext+=key;
      }
    }
    
  • Thank you very much. I only posted part of the code so it would be easier. Problem was with the definition of "tf". I can know write without interfering with the commands

    if ((key == 'a') && (tf.isFocus()==false)) {
    //code
    }
    
Sign In or Register to comment.