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

**URL:** <https://discourse.processing.org/t/keypressed-event-does-not-trigger-with-java-project-referencing-core-jar/41655>\
**Category:** Beginners\
**Created:** [April 8, 2023, 11:30am UTC](https://discourse.processing.org/t/keypressed-event-does-not-trigger-with-java-project-referencing-core-jar/41655 "2023-04-08T11:30:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjamin-thomas](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/benjamin-thomas/32/18241_2.png) [@benjamin-thomas](https://discourse.processing.org/u/benjamin-thomas)\
**Post date:** [April 8, 2023, 11:30am UTC](https://discourse.processing.org/t/keypressed-event-does-not-trigger-with-java-project-referencing-core-jar/41655/1 "2023-04-08T11:30:39Z")

</div>

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.

```auto
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:

```auto
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](https://github.com/benjamin-thomas/nature-of-code/tree/1accab0e10c7795c681111d644678d106ec47120/_basic_setup/java/processing_maven)

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?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 8, 2023, 3:21pm UTC](https://discourse.processing.org/t/keypressed-event-does-not-trigger-with-java-project-referencing-core-jar/41655/2 "2023-04-08T15:21:36Z")

</div>

> [@benjamin-thomas](#):
>
> I’d like to play with Processing, but I’d favor using IntelliJ rather than the custom IDE.

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:

```auto
@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_:

> **[frameCount / Reference](https://processing.org/reference/frameCount.html)**
>
> The system variable frameCount contains the number o frames displayed since the program started. Inside setup() the value is 0 and during the first iteration of draw it is 1, etc.

Callback **draw()** is being run once only.

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

> <https://github.com/benfry/processing4/blob/processing-1292-4.2/core/src/processing/core/PApplet.java#L1530-L1534>

```auto
  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:

```auto
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");
  }
}

```

---

<div class="post-metadata">

**Author:** ![benjamin-thomas](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/benjamin-thomas/32/18241_2.png) [@benjamin-thomas](https://discourse.processing.org/u/benjamin-thomas)\
**Post date:** [April 8, 2023, 4:04pm UTC](https://discourse.processing.org/t/keypressed-event-does-not-trigger-with-java-project-referencing-core-jar/41655/3 "2023-04-08T16:04:45Z")

</div>

> [@GoToLoop](#):
>
> And the culprit is this completely unnecessary line `super.draw();`, which then executes this:

Well that was dumb 🙂 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.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 8, 2023, 4:16pm UTC](https://discourse.processing.org/t/keypressed-event-does-not-trigger-with-java-project-referencing-core-jar/41655/4 "2023-04-08T16:16:14Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![benjamin-thomas](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/benjamin-thomas/32/18241_2.png) [@benjamin-thomas](https://discourse.processing.org/u/benjamin-thomas)\
**Post date:** [April 8, 2023, 5:27pm UTC](https://discourse.processing.org/t/keypressed-event-does-not-trigger-with-java-project-referencing-core-jar/41655/5 "2023-04-08T17:27:18Z")

</div>

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