Sample code to use mouse wheel events in Processing.

// mouseWheel.pde
// code to use mouse wheel in Processing

// must import java.awt.event.*
import java.awt.event.*;

float y;

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

  // add mouse wheel listener
  frame.addMouseWheelListener(new MouseWheelInput());

  y=height/2;
}

void draw() {
  background(100);
  ellipse(width/2, y, 50,50);
}

// convenience class to listen for MouseWheelEvent
// modify mouseWheelMoved() for your own purposes

class MouseWheelInput implements MouseWheelListener{

  void mouseWheelMoved(MouseWheelEvent e) {
    int step=e.getWheelRotation();
    y=y+step*5;
  }

}

5 Comments »

There are 5 comments to "Code: mouseWheel.pde". You may leave your own comment.
1. Kyle McDonald, September 28th, 2007 at 16:09

Nice!

2. Ang, June 7th, 2009 at 21:38

Is it possible to have the mouse wheel work in a applet exported from a processing sketch? I’ve tried to exported the code above but the canvas is blank. Am I missing some files?

3. camelo, September 24th, 2010 at 22:53

And for lateral movement, like in MAC mouses?

4. marius watz, September 26th, 2010 at 23:18

Camelo: Java currently does not support horizontal scrolling. But you could try to detect the mouse wheel button being pressed and implement a horizontal scrolling behavior accordingly. Note that mouse wheel buttons aren’t guaranteed to be consistent across platforms, though.

void mousePressed() {
int button=mouseEvent.getButton();
if(button==MouseEvent.BUTTON1) println("Button 1");
if(button==MouseEvent.BUTTON2) println("Button 2");
if(button==MouseEvent.BUTTON3) println("Button 3");
}

5. camelo, September 27th, 2010 at 17:13

thanks man! i’ll try it.
by the way, great site! nice projects.

Comment on this entry

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">