GLUT Tutorial – How to detect a Mouse Click and Mouse Motion

In previous tutorial, we discussed about handling Keyboard event using GLUT. In this tutorial we pay our close attention towards Detecting 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
SHARE GLUT Tutorial – How to detect a Mouse Click and Mouse Motion

You may also like...

6 Responses

  1. Really trustworthy blog. Not only is this a well-written post, but I love the topic. Thanks for sharing !

  2. very helpful to understand, but i have a question, do i call this function before or after the display?, for example if i want to draw a line like paint does. Thank you 🙂

  3. u should atleast provide some examples for the biginners

  4. Now I really understand what mouse function is.

  5. Anonymous says:

    baal bujjho tumi

Leave a Reply

Your email address will not be published. Required fields are marked *