ControlP5 and keyPressed()

Hello fellow Processors!

I have an issue with CP5 and I’d appreciate any thoughts.

I’m using a text field:

gui.addTextfield("frames")
    .setPosition(100, 400)
    .setSize(30, 30)
    .setFont(font)
    .setValue(1)
    .setAutoClear(false)
    ;

and when I put the mouse cursor the field and type a key, as well as the CP5 event happening, which I catch and process with controlEvent(), the regular keyPressed() also gets triggered with the key event. This seems strange, and undesirable! Is it necessary to have explicit logic (checking mouse pos not in CP5 area) to avoid this?

Thanks!
Toby

Hi @toby

You can use the .isActive method on the controller to check if the cp5 controller is active. If you this method on the keyPressed event then you can shift between the desirable actions.

Check the example below

import controlP5.*;

ControlP5 cp5;

String textValue = "";

void setup() {
  size(700,400);
  
  PFont font = createFont("arial",20);
  
  cp5 = new ControlP5(this);
  
  cp5.addTextfield("input")
     .setPosition(20,100)
     .setSize(200,40)
     .setFont(font)
     .setFocus(true)
     .setColor(color(255,0,0))
     ;
     
  textFont(font);
}

void draw() {
  background(0);
  fill(255);
  text(cp5.get(Textfield.class,"input").getText(), 360,130);
  text(textValue, 360,180);
}


void keyPressed(){
  if(!cp5.getController("input").isActive()) println(key); //if controller not active
  else println("You press but I dont want to tell you the key");
}

Hooe it helps,
Best regards

2 Likes

Thanks a million Miguel! That works perfectly!

best wishes!

1 Like