# mousePressed() and MouseDragged() pressed key in println

**URL:** <https://discourse.processing.org/t/mousepressed-and-mousedragged-pressed-key-in-println/25241>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [November 6, 2020, 2:14pm UTC](https://discourse.processing.org/t/mousepressed-and-mousedragged-pressed-key-in-println/25241 "2020-11-06T14:14:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ellie98](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@Ellie98](https://discourse.processing.org/u/Ellie98)\
**Post date:** [November 6, 2020, 2:14pm UTC](https://discourse.processing.org/t/mousepressed-and-mousedragged-pressed-key-in-println/25241/1 "2020-11-06T14:14:02Z")

</div>

Hello,

I have to show the keys with println that I pressed if I use the functions keyPressed(), mousePressed() and mouseDragged(). I used println and key. It’s showing up the keys I clicked on my keyboard for keyPressed() but it’s just showing 0 for mousePressed() and mouseDragged(). Do I have to use something else then “key” to get the number of the mousekey I pressed?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [November 6, 2020, 2:52pm UTC](https://discourse.processing.org/t/mousepressed-and-mousedragged-pressed-key-in-println/25241/2 "2020-11-06T14:52:54Z")

</div>

Use LEFT, RIGHT, and CENTER. See [here.](https://processing.org/tutorials/interactivity/)

---

<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:** [November 6, 2020, 3:36pm UTC](https://discourse.processing.org/t/mousepressed-and-mousedragged-pressed-key-in-println/25241/3 "2020-11-06T15:36:40Z")

</div>

A key can be

- the button / key on the keyboard OR
- the button on the mouse

**Keyboard**

use key and:

> The variable **keyCode** is used to detect special keys such as the arrow keys (UP, DOWN, LEFT, and RIGHT) as well as ALT, CONTROL, and SHIFT.

from [keyCode / Reference / Processing.org](https://www.processing.org/reference/keyCode.html)

**Mouse**

quote:

> When a mouse button is pressed, the value of the system variable **mouseButton** is set to either **LEFT** , **RIGHT** , or **CENTER**

from [mouseButton / Reference / Processing.org](https://www.processing.org/reference/mouseButton.html)

Chrisir

---

<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:** [November 6, 2020, 3:39pm UTC](https://discourse.processing.org/t/mousepressed-and-mousedragged-pressed-key-in-println/25241/4 "2020-11-06T15:39:20Z")

</div>

another remark:

> [@Ellie98](#):
>
> it’s just showing 0 for mousePressed()

mousePressed() is called only once and very briefly. So when you are using background() and text() command you won’t see much.

- Maybe you register the key in a variable and show the variable with text()

---

<div class="post-metadata">

**Author:** ![Ellie98](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@Ellie98](https://discourse.processing.org/u/Ellie98)\
**Post date:** [November 6, 2020, 3:59pm UTC](https://discourse.processing.org/t/mousepressed-and-mousedragged-pressed-key-in-println/25241/5 "2020-11-06T15:59:20Z")

</div>

I wrote it like this:

```auto
void draw() {
}
 void mouseDragged() {
  println("mouseDragged():", key);
  text("test", mouseX, mouseY);
}

```

It worked with keyPressed() perfectly but i’m just seeing 0 whatever key I use on my mouse to drag

---

<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:** [November 6, 2020, 4:08pm UTC](https://discourse.processing.org/t/mousepressed-and-mousedragged-pressed-key-in-println/25241/6 "2020-11-06T16:08:01Z")

</div>

> [@Chrisir](#):
>
> > When a mouse button is pressed, the value of the system variable **mouseButton** is set to either **LEFT** , **RIGHT** , or **CENTER**
> 
> from [mouseButton / Reference / Processing.org](https://www.processing.org/reference/mouseButton.html)

Please read what noel wrote and what I wrote

In theory you could have if…else if… to evaluate mouseButton

---

<div class="post-metadata">

**Author:** ![Ellie98](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@Ellie98](https://discourse.processing.org/u/Ellie98)\
**Post date:** [November 6, 2020, 6:29pm UTC](https://discourse.processing.org/t/mousepressed-and-mousedragged-pressed-key-in-println/25241/7 "2020-11-06T18:29:08Z")

</div>

mouseButtom worked, thanks!
