# Use Mouse-funktions in classes

**URL:** https://discourse.processing.org/t/use-mouse-funktions-in-classes/29673
**Category:** Coding Questions
**Created:** [April 27, 2021, 5:09am UTC](https://discourse.processing.org/t/use-mouse-funktions-in-classes/29673 "2021-04-27T05:09:55Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![TimeLex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timelex/32/12869_2.png) [@TimeLex](https://discourse.processing.org/u/TimeLex)
#### Post date: [April 27, 2021, 5:09am UTC](https://discourse.processing.org/t/use-mouse-funktions-in-classes/29673/1 "2021-04-27T05:09:55Z")

</div>

I want to write a class that is able to use Mouse-funktions like mousePressed() but the funktion is not triggered in a class:

```auto
myClass c;

void setup() {
  size(500, 500);
  
  c = new myClass();
}

void draw() {
  c.update();
}

class myClass{
  void update() {
    
  }
  
  void mousePressed() {
    println("mouse was pressed at frame "+frameCount);
  }
}

```

Until now I solved the problem like this:

```auto
void mousePressed() {
  c.mousePressed();
}

```

is there a way that the class can also start the functions on its own?

---

<div class="post-metadata">

### Author: ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)
#### Post date: [April 27, 2021, 8:54am UTC](https://discourse.processing.org/t/use-mouse-funktions-in-classes/29673/2 "2021-04-27T08:54:49Z")

</div>

Use `registerMethod()`.

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e204c6d22478ae855192be624a2251f4541f78b6.png)

**Example**

Create a ` **public** void mouseEvent(){}` method in your class and call `p.registerMethod("mouseEvent", this);` during the class object’s initialisation, where `p` is the parent PApplet.

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [April 27, 2021, 8:58am UTC](https://discourse.processing.org/t/use-mouse-funktions-in-classes/29673/3 "2021-04-27T08:58:52Z")

</div>

```auto
myClass c;

void setup() {
  size(500, 500);

  c = new myClass(this);
}

void draw() {
  c.update();
}

public class myClass {
  myClass(PApplet p) {
    p.registerMethod("mouseEvent", this);
  }

  void update() {
  }

  void mousePressed() {
    println("mouse was pressed at frame "+frameCount);
  }

  public void mouseEvent(MouseEvent e) {
    if (e.getAction() == MouseEvent.PRESS) {
      println("mouse pressed at " + e.getX() + " " + e.getY());
    }
  }
}

```

it’s actually quite complicated ☹

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 27, 2021, 9:24am UTC](https://discourse.processing.org/t/use-mouse-funktions-in-classes/29673/4 "2021-04-27T09:24:14Z")

</div>

For a small project I don’t know why making it so complicated.

Why not just calling the function through the mouseClicked() function?

```auto
myClass c;

void setup() {
  size(500, 500);

  c = new myClass(this);
}

void draw() {
  c.update();
}

void mouseClicked() {
  c.myEvent();
}

public class myClass {
  myClass(PApplet p) {
    p.registerMethod("mouseEvent", this);
  }

  void update() {
  }

  void myEvent() {
    println("mouse was pressed at frame "+frameCount);
  }
}

```

---

<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 27, 2021, 10:38am UTC](https://discourse.processing.org/t/use-mouse-funktions-in-classes/29673/5 "2021-04-27T10:38:03Z")

</div>

> [@TimeLex](#):
>
> … but the function is not triggered in a class:

> [@Handle (and consume) mouse events in classes](https://discourse.processing.org/t/handle-and-consume-mouse-events-in-classes/17689/4):
>
> To my surprise, Processing doesn’t directly call back input events such as mousePressed(), keyReleased(), etc. open_mouth But only these 2 input events: mouseEvent() & keyEvent(). drooling_face So we can only use those 2 for registerMethod() as input events: /\*\* \* RegisterMethod MouseEvent (v1.0) \* GoToLoop (2020/Feb/10) \* \* https://Discourse.Processing.org/t/ \* handle-and-consume-mouse-events-in-classes/17689/4 \* \* https://GitHub.com/processing/processing/wiki/ \* Library-Basics#w…

---

<div class="post-metadata">

### Author: ![TimeLex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timelex/32/12869_2.png) [@TimeLex](https://discourse.processing.org/u/TimeLex)
#### Post date: [April 27, 2021, 2:32pm UTC](https://discourse.processing.org/t/use-mouse-funktions-in-classes/29673/6 "2021-04-27T14:32:33Z")

</div>

Tanks, it works cery well.

---

<div class="post-metadata">

### Author: ![TimeLex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timelex/32/12869_2.png) [@TimeLex](https://discourse.processing.org/u/TimeLex)
#### Post date: [April 27, 2021, 2:35pm UTC](https://discourse.processing.org/t/use-mouse-funktions-in-classes/29673/7 "2021-04-27T14:35:55Z")

</div>

I’m trying to make a class that specifies buttons and other things that are also automatically pressed. Since I will need this class more often, it is very cumbersome to write it down for each funktion.

---

<div class="post-metadata">

### Author: ![TimeLex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timelex/32/12869_2.png) [@TimeLex](https://discourse.processing.org/u/TimeLex)
#### Post date: [April 29, 2022, 5:09pm UTC](https://discourse.processing.org/t/use-mouse-funktions-in-classes/29673/8 "2022-04-29T17:09:11Z")

</div>

btw you can omit the PApplet parameter in the constructor of `myClass` and just do `registerMethod("mouseEvent", this);`.
