# Functions outside draw? the midibus

**URL:** https://discourse.processing.org/t/functions-outside-draw-the-midibus/25464
**Category:** Libraries
**Created:** [November 16, 2020, 4:21pm UTC](https://discourse.processing.org/t/functions-outside-draw-the-midibus/25464 "2020-11-16T16:21:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Eyetoof](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@Eyetoof](https://discourse.processing.org/u/Eyetoof)
#### Post date: [November 16, 2020, 4:21pm UTC](https://discourse.processing.org/t/functions-outside-draw-the-midibus/25464/1 "2020-11-16T16:21:14Z")

</div>

Hello I have a very basic question but would love some clarification.

I have a project that will utilize Midi, I have installed theMidiBus and it is all working, I found a tutorial that helped me but I am confused about something.

In the examples he builds a function below draw to listen to midi input and print the values to the screen.

What I don’t understand is that he does not call this function from with in draw. And yet its live and active when running a very simple sketch. Is it due to theMidiBus library, is it something else? I sure feel like I have written a function and then not had it work because no one was actually calling it, via update or some such.

/void setup() {  
size(800, 600);  
background(255);  
myBus = new MidiBus(this, 1, -1); //Create a new MidiBus with input1 and the default Java Sound Synthesizer as the output device.  
}

void draw() {  
fill(125);  
rect(x, y, 30, 30);  
}

void controllerChange(int channel, int number, int value) {  
// Receive a controllerChange  
println();  
println(“Controller Change:”);  
println("--------");  
println(“Channel:”+channel);  
println(“Number:”+number);  
println(“Value:”+value);  
}/

---

<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: [November 16, 2020, 5:26pm UTC](https://discourse.processing.org/t/functions-outside-draw-the-midibus/25464/2 "2020-11-16T17:26:22Z")

</div>

> [@Eyetoof](#):
>
> What I don’t understand is that he does not call this function from within **draw()**.

[http://discourse.processing.org/faq#format-your-code](http://discourse.processing.org/faq#format-your-code)

That’s known as a callback function.

Even **setup()**, **draw()**, etc. are also callback functions.

You need to read a library documentation in order to know whether it features any callbacks.

---

<div class="post-metadata">

### Author: ![Eyetoof](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@Eyetoof](https://discourse.processing.org/u/Eyetoof)
#### Post date: [November 17, 2020, 6:27pm UTC](https://discourse.processing.org/t/functions-outside-draw-the-midibus/25464/3 "2020-11-17T18:27:12Z")

</div>

Thank you, that makes sense. I appreciate your help.

---

<div class="post-metadata">

### Author: ![Eyetoof](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@Eyetoof](https://discourse.processing.org/u/Eyetoof)
#### Post date: [November 20, 2020, 4:43pm UTC](https://discourse.processing.org/t/functions-outside-draw-the-midibus/25464/4 "2020-11-20T16:43:25Z")

</div>

So then is

```auto
keyPressed() ;

```

a call back function as well?

---

<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: [November 20, 2020, 7:12pm UTC](https://discourse.processing.org/t/functions-outside-draw-the-midibus/25464/5 "2020-11-20T19:12:29Z")

</div>

Yes, **keyPressed()** is indeed a callback method from class PApplet: ⌨

- [Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#keyPressed--](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#keyPressed--)

However **controllerChange()** isn’t actually predefined within class PApplet. 😶

The class MidiBus needs to use Java reflective techniques in order to introspect whether or not we had defined a **controllerChange()** callback method within our PApplet subclass sketch: 🔍

> **[java.lang.reflect (Java SE 11 & JDK 11 )](https://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/package-summary.html)**

---

<div class="post-metadata">

### Author: ![Eyetoof](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@Eyetoof](https://discourse.processing.org/u/Eyetoof)
#### Post date: [November 21, 2020, 12:23am UTC](https://discourse.processing.org/t/functions-outside-draw-the-midibus/25464/6 "2020-11-21T00:23:43Z")

</div>

So good! THANK YOU

I will dig into this!
