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.
Example
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);
W.display(window::center,window::center);
W.draw_disc(0,0,5,red);
W.read_mouse();
W.screenshot("trivial_mouse_input");
return 0;
}
|
|
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. |
Example
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.
|
|
#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;
p=point(x,y);
return ran;
}
int main()
{
window W(400,400);
W.display(window::center,window::center);
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)) {
W.screenshot("get_mouse");
W.clear();
}
point p;
ran >> p;
W.draw_point(p,blue);
}
return 0;
}
|
Remark: There are also more general input operations
available for window events. |
|
See also:
Windows and Panels
Panel Items
Buttons
Window Events
Drawing Operations
Basic Data Types for 2D Geometry
Manual Pages:
Manual
Page Windows
|