# Having keyPressed() in multiple classes

**URL:** https://discourse.processing.org/t/having-keypressed-in-multiple-classes/26401
**Category:** Beginners
**Created:** [December 20, 2020, 3:24pm UTC](https://discourse.processing.org/t/having-keypressed-in-multiple-classes/26401 "2020-12-20T15:24:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![gapigapi](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@gapigapi](https://discourse.processing.org/u/gapigapi)
#### Post date: [December 20, 2020, 3:24pm UTC](https://discourse.processing.org/t/having-keypressed-in-multiple-classes/26401/1 "2020-12-20T15:24:55Z")

</div>

> I am having some problems with void keypress() function. If i use it in the main class it works, but if i then try to call a class where i do the maths and then draw() which works in that class and draw on canvas created in main class, but the keypress function doesn’t trigger when i type in that class. I tried to add another setup() in that class but it didn’t change anything.  
> The problem is also that if i want to change things from main class by keypress is that i have to always create a new clusters class or i can’t call it’s functions but i don’t want to do that. I want to change the class that i already created.  
> \< code:  
> class clusters (){  
> void draw(){  
> draws on canvas…  
> }  
> void keyPressed(){  
> print(key);  
> switch(key){  
> case ‘x’:  
> zoom+=0.5;  
> firstdraw=true;  
> break;  
> case ‘y’:  
> zoom-=0.5;  
> firstdraw=true;  
> break;  
> }  
> }  
> } \<

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [December 20, 2020, 4:58pm UTC](https://discourse.processing.org/t/having-keypressed-in-multiple-classes/26401/2 "2020-12-20T16:58:12Z")

</div>

> [@gapigapi](#):
>
> If i use it in the main class it works, but if i then try to call a class where i do the maths

Yeah, keys are not automatically passed to the class(es) - you have to call your keyPressed located inside the class from the main keyPressed. I can make an example if you want.

in the main part of the Sketch

```auto
void keyPressed() {
   player.keyPressed(); 
}

```

in the Player class :

```auto
void keyPressed() {

    switch(key) {
    case '1':
      //
      break; 
    }//switch 
}//method

```

> [@gapigapi](#):
>
> The problem is also that if i want to change things from main class by keypressed is that i have to always create a new clusters class or i can’t call it’s functions but i don’t want to do that.

Not sure if I understand you there. To make a new class or new objects from the class is not the right way. Instead you change its values by saying player.x++, (call this ouside the class) or make a method moveRight() in the class and call it like player.moveRight();

**Hey, and welcome to the forum!**

**Great to have you here!**

Warm regards,

Chrisir 😉

P.S.

Here is a full example:

```auto

Clusters c = new Clusters();  

void setup() {
  // init
  size(1300, 700);
} // func 

void draw() {
  // runs on and on 
  background(0);
  c.draw();
}

void keyPressed() {
  c.keyPressed();
}

// ===============================================================

class Clusters {

  float x=300, y=300; 

  void draw() {
    //draws on canvas…
    ellipse(x, y, 18, 18);
  }

  void keyPressed() {
    print(key);
    switch(key) {
    case 'x':
      x+=5;
      //firstdraw=true;
      break;
    case 'y':
      y-=0.5;
      //firstdraw=true;
      break;
    }//switch
  }//func
}//class 
//

```

---

<div class="post-metadata">

### Author: ![gapigapi](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@gapigapi](https://discourse.processing.org/u/gapigapi)
#### Post date: [December 20, 2020, 6:53pm UTC](https://discourse.processing.org/t/having-keypressed-in-multiple-classes/26401/3 "2020-12-20T18:53:30Z")

</div>

Well u get errors saying the sketch path is not set.  
And then about 20 lines where it crashes and at the end runtimeexception: java.lang.reflect.invocationtargetexception.

FIXED IT:  
needed to only initialize the variable above setup as \<player p and then in setup() p=new player() \> maybe because i had arguments that i put to class that the error had acoured.
