View Full Version : mouse events

03-29-2001, 05:24 AM
hello i'm new to opengl and I try to make a little prog where i can turn, translate and zoom with the mouse. How can i "catch" mouse's events ???

(sorry for my english, i'm french)

03-29-2001, 06:34 AM
glutMotionFunc, glutPassiveMotionFunc
glutMotionFunc and glutPassiveMotionFunc set the motion and passive motion callbacks respectively for the current window.


void glutMotionFunc(void (*func)(int x, int y));
void glutPassiveMotionFunc(void (*func)(int x, int y));

The new motion or passive motion callback function.


glutMotionFunc and glutPassiveMotionFunc set the motion and passive motion callback respectively for the current window. The motion callback for a window is called when the mouse moves within the window while one or more mouse buttons are pressed. The passive motion callback for a window is called when the mouse moves within the window while no mouse buttons are pressed.

The x and y callback parameters indicate the mouse location in window relative coordinates.

Passing NULL to glutMotionFunc or glutPassiveMotionFunc disables the generation of the mouse or passive motion callback respectively.


glutMouseFunc sets the mouse callback for the current window.


void glutMouseFunc(void (*func)(int button, int state,
int x, int y));

The new mouse callback function.


glutMouseFunc sets the mouse callback for the current window. When a user presses and releases mouse buttons in the window, each press and each release generates a mouse callback. The button parameter is one of GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON. For systems with only two mouse buttons, it may not be possible to generate GLUT_MIDDLE_BUTTON callback. For systems with a single mouse button, it may be possible to generate only a GLUT_LEFT_BUTTON callback. The state parameter is either GLUT_UP or GLUT_DOWN indicating whether the callback was due to a release or press respectively. The x and y callback parameters indicate the window relative coordinates when the mouse button state changed. If a GLUT_DOWN callback for a specific button is triggered, the program can assume a GLUT_UP callback for the same button will be generated (assuming the window still has a mouse callback registered) when the mouse button is released even if the mouse has moved outside the window.

If a menu is attached to a button for a window, mouse callbacks will not be generated for that button.

During a mouse callback, glutGetModifiers may be called to determine the state of modifier keys when the mouse event generating the callback occurred.

Passing NULL to glutMouseFunc disables the generation of mouse callbacks.

03-29-2001, 06:36 AM
Thanks but I don't want (have to) use glut, only winapi.

03-29-2001, 06:49 AM
You don't say what OS you are using so I'll assume Windows. OpenGL itself doesn't have anything for catching mouse events. If you are using Windows just check for the Windows mouse events. WM_LBUTTONDOWN, WM_MOUSEMOVE, WM_RBUTTONDOWN, etc., etc..

03-29-2001, 06:58 AM
Thanks again.
Your alright, i'm using windows. I've just found the mouse event but i still have some problems. Where to found a description of each event. I cant find something like wm_lmouseboutton_hold_down , i say it doesn't exist but i want something which detect that the mouse button is pressed. (i'm note sure to be clear). I also need to detect when the left and right button are pressed together.

03-29-2001, 07:02 AM

" i say it doesn't exist " you have to read KNOW instead of SAY

03-29-2001, 07:51 AM
Keep track of the state of the mouse buttons yourself. For instance, on a WM_LBUTTONDOWN, set the left button state to true, if you get a WM_LBUTTONUP, set the state to false, during the WM_MOUSEMOVE, check the states to see if the buttons are down.

03-29-2001, 11:19 AM
GetAsynchKeyState(VK_LBUTTON), GetAsynchKeyState(VK_RBUTTON) and GetAsynchKeyState(VK_MBUTTON) will tell you whether any of the mouse buttons are pressed without having to use the messaging loop.

03-30-2001, 03:32 AM
Thanks but i think i've got to include a .h or a lib to use those command ???

03-30-2001, 03:36 AM
Your method don't seems so good

Use of the GetAsynchKeyState Function

The GetAsynchKeyState function should not be used to retrieve the state of a key or a mouse button, except when absolutely necessary. This function queries the hardware driver to determine the physical key or button state, but ignores any keys being artificially held down or simulated by accessibility aids. When possible, you should use the GetKeyState function, which correctly reflects any simulated input. GetAsynchKeyState can, however, be called in certain cases, such as when the user types a key to interrupt a lengthy processing task.

When handling mouse drag operations, you should avoid using GetAsynchKeyState to detect when a mouse button has been lifted. Instead, you should use the SetCapture function and wait for a button-up message. If you use GetAsynchKeyState, the results might differ from those obtained using other Windows functions and messages. This can cause your application to behave inconsistently with other software on the system.

03-30-2001, 09:18 AM
Is this MFC function:-


03-30-2001, 10:05 AM
Those are Win32 messages. MFC uses virtual functions that can be over-ridden in any of the CWnd derived classes to handle these message. For instance, OnLButtonDown() will handle the WM_LBUTTONDOWN message for you. In class view just right click the CWnd class you want to use (usually a view class, though it doesn't necessarily have to be) and choose Windows Message Handlers. It then gives you a list of window messages you can handle, and you just double click them to handle them.

If you don't understand this stuff, you really should learn it before learning OpenGL.