070926 | Code, Processing / Java
Tags: Code, hack, input, java, mouse, mousewheel, pde, processing.org
Tags: Code, hack, input, java, mouse, mousewheel, pde, processing.org
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;
}
}





Nice!
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?
And for lateral movement, like in MAC mouses?
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");
}
thanks man! i’ll try it.
by the way, great site! nice projects.