`keyPressed` event does not trigger with Java project referencing `core.jar`

Hello!

I’d like to play with Processing, but I’d favor using IntelliJ rather than the custom IDE.

I’ve managed to get a basic project running, however the key events don’t seem to register.

Here’s a minimal example.

import processing.core.PApplet;

public class Main extends PApplet {
    @Override
    public void setup() {
        background(0);
        System.out.println("setup!");
        super.setup();
    }

    @Override
    public void draw() {
        stroke(0, 0, 255);
        fill(255, 0, 0);
        circle((float) width /2, (float) height /2, 20);
        super.draw();
    }

    @Override
    public void keyPressed() {
        System.out.println("keyPressed!");
        super.keyPressed();
    }

    public static void main(String[] args) {
        PApplet.main("Main");
    }
}

I can see the drawing alright, but my program doesn’t register any key strokes and only prints:

setup!

Process finished with exit code 0 // <-- once I manually closed the window

The starter project is available here: nature-of-code/_basic_setup/java/processing_maven at 1accab0e10c7795c681111d644678d106ec47120 · benjamin-thomas/nature-of-code · GitHub

I also built a new project without maven, and manually referenced core.jar from my downloaded processing-4.2 folder: same result - I can see no key being registered.

Can anybody enlighten me?

Before moving to other IDEs we should still need to get acquainted w/ how things are done on Processing’s IDE (PDE).

My guess is that all of those public, @Override, System.out., super., (float), etc., are “smart” hints that you’ve blindly accepted, right?

Out of those I believe only public is required; at least for Processing’s callbacks.

I’ve made some tweaks so your code is able to run on the PDE:

@Override public void setup() {
  frameRate(1);
  background(0);
  System.out.println("setup!");
  super.setup();
}

@Override public void draw() {
  stroke(0, 0, 255);
  fill(255, 0, 0);
  circle((float) width / 2, (float) height / 2, 20);
  System.out.println(frameCount);
  super.draw();
}

@Override public void keyPressed() {
  System.out.println("keyPressed!");
  super.keyPressed();
}

And surprise! It fails on the PDE too!

It’s made even more evident when we also log variable frameCount:

Callback draw() is being run once only.

And the culprit is this completely unnecessary line super.draw();, which then executes this:

  public void draw() {
    // if no draw method, then shut things down
    //System.out.println("no draw method, goodbye");
    finished = true;
  }

I’ve refactored your minimal example so it satisfies both Java’s syntax & Processing’s code style:

import processing.core.PApplet;

public class Main extends PApplet {
  static final int DIAM = 20;

  public void setup() {
    frameRate(1);
    stroke(0, 0, 255);
    fill(255, 0, 0);
    println("setup!");
  }

  public void draw() {
    background(0);
    circle(width >> 1, height >> 1, DIAM);
    print(frameCount, TAB);
  }

  public void keyPressed() {
    println("\nkeyPressed!", key, keyCode);
  }

  public static void main(String[] args) {
    main("Main");
  }
}
2 Likes

Well that was dumb :slight_smile: thanks @GoToLoop!

I was aware my super calls were mostly NOOPs.

But I somehow missed reading that comment and assumed that finished=true was referring to internal engine state, for the current loop.

I also assumed pde → java conversion would incur this boilerplate but that doesn’t seem to be the case at all.

rm -rf /tmp/sketch ; processing-java --sketch=$PWD --output=/tmp/sketch --export && tail -n+1 /tmp/sketch/source/*.java

Thanks for letting me know about frameCount, that’ll be useful.

1 Like

Bonus tips for (float) cast operator interaction w/ Processing’s library:

  • We only need it if we end up w/ a double datatype expression.
  • Otherwise, Java can automatically coerce any other primitive datatype (except boolean) to float.
  • So make sure to declare float variables in place of double.
  • And always suffix w/ an f any number literal containing a decimal . dot or exponential e.
  • For example, literal 9.3 should be typed in as 9.3f; and 1e-3 as 1e-3f.
1 Like

Using the suffix notation is much better, thanks for the tips!