mousePressed() and MouseDragged() pressed key in println


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!

Use LEFT, RIGHT, and CENTER. See here.

A key can be

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


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.




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



another remark:

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()

I wrote it like this:

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

Please read what noel wrote and what I wrote

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

1 Like

mouseButtom worked, thanks!

1 Like