Double-click

From Processing

Jump to: navigation, search
Versions: 1.0+
Contributors: toxi
Started: 2006-01-31


There are two ways to get a closer peek at input events in Processing. Both have their advantages, but since one of them is more hackish than the other we'll be demonstrating it first.

Contents

Source Code

Solution 1: Using the mouseEvent variable

In the depths of PApplet is a public variable called mouseEvent storing a reference to the current (or most recent) mouse event for internal purposes. You can query this event object for more information from within your mousePressed(), mouseReleased() etc. handlers and that way find out (amongst other things) if a user has clicked a mouse button several times.

/**
doubleclick taken from http://wiki.processing.org/index.php?title=Double-click
demonstrates checking for double click and extra mouse button events
using PApplet.mouseEvent
@author toxi
*/
void mousePressed() {
  // mouseEvent variable contains the current event information
  if (mouseEvent.getClickCount()==2) println("<double click>");
  if (mouseEvent.getButton()==MouseEvent.BUTTON3) println("<right button>");
  // this prints out the event in descriptive form
  println(mouseEvent);
}

Solution 2: Overwriting event handlers

Processing is internally using special versions of the various mouse and keyboard event handling functions. You can overwrite those yourself to retrieve more detailed information about the event just occurred. Unlike the normal mousePressed(), mouseReleased(), mouseDragged(), mouseMoved() functions you force them to require a MouseEvent object as parameter… Those event objects are prepopulated by Java and can be queried for various properties usually unavailable in Processing, including modifier keys pressed or multiple mouse buttons pressed etc.

IMPORTANT: Using these types of event handlers will result in a slightly different interaction and visual behaviour compared to the standard Processing implementation. Processing usually is queueing up input events until the draw() function has finished executing for the current frame. That way mouse events will not cause any visual changes introduced by repeatedly checking mouse states within your draw() code. Because they're running in a different thread custom mouse handlers as shown below actually overwrite this functionality provided by Processing and can interrupt the draw() function. Java will call these functions as soon as the event occurs, without any delay.

Using this type of event handling also disables any updates to the mousePressed and mouseButton Processing variables. If you wish you could add the line super.mousePressed(e); to the function below in order to pass on the event to Processing. In that case you have to make sure you don't handle events twice, though.

/**
doubleclick taken from http://wiki.processing.org/index.php?title=Double-click
demonstrates checking for double click and extra mouse button events
using standard Java AWT event handlers
@author toxi
*/
 
void mousePressed(MouseEvent e) {
  if (e.getClickCount()==2) println("<double click>");
  if (e.getButton()==MouseEvent.BUTTON3) println("<right button>");
  // this prints out the event in descriptive form
  println(e);
}

Downloads

Related Links

Personal tools