# Placement of keyPressed function

**URL:** <https://discourse.processing.org/t/placement-of-keypressed-function/21604>\
**Category:** Processing.py\
**Created:** [June 5, 2020, 6:49pm UTC](https://discourse.processing.org/t/placement-of-keypressed-function/21604 "2020-06-05T18:49:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarlook](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jarlook](https://discourse.processing.org/u/jarlook)\
**Post date:** [June 5, 2020, 6:49pm UTC](https://discourse.processing.org/t/placement-of-keypressed-function/21604/1 "2020-06-05T18:49:30Z")

</div>

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.

---

<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:** [June 5, 2020, 10:41pm UTC](https://discourse.processing.org/t/placement-of-keypressed-function/21604/2 "2020-06-05T22:41:11Z")

</div>

> [@jarlook](#):
>
> There’s something about the Processing provided keyPressed() function that appears to be the problem.

**keyPressed()**, **setup()**, **draw()**, etc. are PApplet callbacks:  
[Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#keyPressed--](http://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:

```auto
from cvplib import imported_def

def keyPressed():
    imported_def()

```

---

<div class="post-metadata">

**Author:** ![jarlook](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jarlook](https://discourse.processing.org/u/jarlook)\
**Post date:** [June 6, 2020, 10:22pm UTC](https://discourse.processing.org/t/placement-of-keypressed-function/21604/3 "2020-06-06T22:22:56Z")

</div>

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.
