Algorithmic Solutions > LEDA > LEDA Guide > Windows and Panels > Mouse Input

Mouse Input

Read Mouse

The main input operations for reading positions, mouse clicks, and buttons from a window W is
  int W.read_mouse();
The operation is blocking, i.e., it waits for a button to be pressed, which is either a "real" button of the mouse or a button in the panel section of W. It returns the number of the selected button. Mouse buttons have predefined numbers

MOUSE_BUTTON(1): left button, MOUSE_BUTTON(2): middle button, MOUSE_BUTTON(3): right button

Numbers of panel buttons can be defined by the user.


The following simple program demonstrates the operation. Any click of a mouse button stops the program. On the right there is a screenshot of the program. Clicking on the picture shows the window in original size.

#include <LEDA/graphics/window.h>

using namespace leda;

int main()
  window W(500,500);W.init(-10,+10,-10);	



  return 0;
Picture of Convex Hull

Get Mouse

There is also a non-blocking input operation
	int W.get_mouse()
It checks whether there is an unprocessed click in the input queue of the window. If a click is available, it will be processed. Otherwise the constant NO_BUTTON is returned.


The following program draws random points. It uses W.get_mouse() at the beginning of every execution of the main loop to check whether a mouse button has been clicked or not. If the right button has been clicked the loop is terminated. If the left button has been clicked the drawing area of W is cleared.

On the right there is a screenshot of the program. Clicking on the picture shows the window in original size.

Picture of Convex Hull
#include <LEDA/window.h>
#include <LEDA/random_source.h>

using namespace leda;

random_source& operator>>(random_source& ran, point& p)
  int x,y;
  ran >> x >> y;
  return ran;

int main()
  window W(400,400);
  W.message("left button: clear    right button: stop");

  random_source ran(0,100);

  int but;
  while ((but=W.get_mouse())!=MOUSE_BUTTON(3)) {
    if (but==MOUSE_BUTTON(1)) {
    point p;
    ran >> p;

  return 0;
Remark: There are also more general input operations available for window events.

See also:

Windows and Panels

Panel Items


Window Events

Drawing Operations

Basic Data Types for 2D Geometry

Manual Pages:

Manual Page Windows

Please send any suggestions, comments or questions to
© Copyright 2001-2003, Algorithmic Solutions Software GmbH