ControlP5 and keyPressed()

Hello fellow Processors!

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

I’m using a text field:

    .setPosition(100, 400)
    .setSize(30, 30)

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?


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() {
  PFont font = createFont("arial",20);
  cp5 = new ControlP5(this);

void draw() {
  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

1 Like

Thanks a million Miguel! That works perfectly!

best wishes!

1 Like