Placement of keyPressed function

I created a keyPressed function. If I place it in the primary tab of the sketch, it works fine. If I place it in a secondary tab (cvplib.py) and import that into the primary tab (import cvplib), it doesn’t work.

I have another function in cvplib.py, which does work. There’s something about the Processing provided keyPressed() function that appears to be the problem.

Any help would be greatly appreciated.

keyPressed(), setup(), draw(), etc. are PApplet callbacks:
Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#keyPressed--

In order for those callbacks to be found by the Processing library, “Python Mode” has to make some hacks behind-the-scenes, so they’re recognized as Java methods from class PApplet.

But “Python Mode” doesn’t know how to turn def functions imported from other “.py” files into Java methods.

However you can invoke imported def functions from within those callbacks on the main “.pyde” tab:

from cvplib import imported_def

def keyPressed():
    imported_def()
2 Likes

Thanks for responding so quickly. For now, I’ve taken the easy way out and included the function in the primary tab. (A small, if inelegant, concession.) I’ll get back to your approach when I have more time.

Again, thanks.

1 Like