How to detect a Mouse Click and Mouse Motion


Detecting Mouse Click
GLUT provides a function glutMouseFunc which is responsible for detecting mouse click generated by a program. Its syntax is
void glutMouseFunc(void (*func)(int button, int state, int x, int y));

func –> Function that handles the mouse event i.e. when mouse click is detected, func is called
func must have three parameters, as we see in above syntax
The first parameter is button, its value is one of the following
  • GLUT_LEFT_BUTTON – when left mouse button click is detected
  • GLUT_RIGHT_BUTTON  – when right mouse button click is detected
  • GLUT_MIDDLE_BUTTON – when middle mouse button click is detected
The second parameter is state, its value is one of the following
  • GLUT_DOWN – When mouse button is pressed
  • GLUT_UP – When mouse button is released
When callback is generated with GLUT_DOWN GLUT environment assumes that GLUT_UP comes afterward even if mouse is not inside the window.
Other two parameters x and y are coordinates of the point where mouse pointer is moved.

Detecting Mouse Motion
GLUT provides two types of function as according to types of motion detection either Active Motion or Passive Motion. Active Motion refers to the Motion of mouse when there is Click and Passive motion refers to motion of a mouse without a click. The syntax of two functions are

glutMotionFunc(void (*func)(int x, int y)); //for Active motion
glutMousePassiveMotionFunc(void (*func)(int x, int y)) // for Passive motion
func –> Function that responsible for handling respective motion
x, y –> coordinates of the mouse relative to upper left corner of window