Home Documentation Download Examples Developer

# Mouse binding customization

## Principles

A mouse click can either perform a given atomic click action (such as selecting an object) or can be the initiation of a mouse action (such as moving the camera). This action is then transmitted to the `camera()`, the `manipulatedFrame()` or the `mouseGrabber()`.

The actual behavior is entirely customizable: the `ClickAction` (triggered when you click) and the `MouseAction` (activated when you click then drag) can be binded to any mouse button(s) with any `Control, Alt, Shift` modifier key combination.

Default bindings are described below. To sum up, the `camera()` is the default mouse event receiver while the `manipulatedFrame()` is used when the `Ctrl` key is pressed.
The `MouseGrabber` is specific since its behavior entirely depends on your implementation. See the MouseGrabber documentation for details.

## Customizing your mouse binding

The following functions are illustrated in the keyboardAndMouse example.

- To bind a `ClickAction`, use `setMouseBinding(uint, ClickAction, doubleClick=false, buttonsBefore=Qt::NoButton)`:

      // Click on the right button to make a selection
      setMouseBinding(Qt::RightButton, SELECT);

      // Pressing the middle button, then double clicking the right button, while pressing Alt shows the entire scene. Cool huh ?
      setMouseBinding(Qt::ALT + Qt::RightButton, SHOW_ENTIRE_SCENE, true, Qt::MidButton);

- To bind a `MouseAction`, use `setMouseBinding(int, MouseHandler, MouseAction, withConstraint=true)` (same function name as above, but different parameters):

      // Left and right buttons together make a camera zoom : emulates a mouse third button if needed.
      setMouseBinding(Qt::LeftButton + Qt::RightButton, CAMERA, ZOOM);

      // Alt + Shift + Left button rotates the manipulatedFrame().
      setMouseBinding(Qt::ALT + Qt::SHIFT + Qt::LeftButton, FRAME, ROTATE);

- And finally, for the mouse wheel associated action, use `setWheelBinding(Qt::KeyboardModifiers, MouseHandler, MouseAction, withConstraint=true)`:

      // Alt + wheel moves the camera forward.
      setWheelBinding(Qt::ALT, CAMERA, MOVE_FORWARD);

The following tables list all the available `ClickAction` and `MouseAction` as well as their default associated bindings. Note that the current bindings are always available in the `Mouse` tab of the help window (press `H` for help).

`ClickAction` | Description | Default binding
`ALIGN_CAMERA` | Align the camera axis with the world coordinate system axis. | Double click left button
`ALIGN_FRAME` | Align the `manipulatedFrame()` axis with the camera. | Control + double click left button
`CENTER_FRAME` | Translates the `manipulatedFrame()` to the center of the screen. | Control + double click right button
`CENTER_SCENE` | Translates the camera so that the `sceneCenter` is in the center of the screen. | Double click right button
`NO_CLICK_ACTION` | No action, only used as a specific return value in `QGLViewer::clickAction()`. |
`SELECT` | Calls the `QGLViewer::select()` function. | Shift + Left button
`RAP_FROM_PIXEL` | Set the camera `revolveAroundPoint()` to the point under pixel (if any). | Double click left button with right button pressed
`RAP_IS_CENTER` | Makes the `sceneCenter` the new camera `revolveAroundPoint()`. | Double click right button with left button pressed
`SHOW_ENTIRE_SCENE` | Translates the camera so that the entire scene is visible. | Double click middle button
`ZOOM_ON_PIXEL` | Makes the camera zoom on the pixel under the mouse (if any). | Double click left button with middle button pressed
`ZOOM_TO_FIT` | Makes the camera zoom to see the entire scene. | Double click right button with middle button pressed

`MouseAction` | `Handler` | Description | Default binding
`NO_MOUSE_ACTION` |  | No action, only used as a specific return value in `QGLViewer::mouseAction()`. |
`ROTATE` | `CAMERA` | Rotates the camera around its `revolveAroundPoint()`. | Left button
`FRAME` | Rotates the `manipulatedFrame()` around its origin. | Control + Left button
`ZOOM` | `CAMERA` | Makes the camera zoom in/out. Speed depends on distance to the scene center. | Middle button
`FRAME` | Makes the manipulatedFrame() move closer or further from the camera. | Control + Middle button
`TRANSLATE` | `CAMERA` | Translates in the camera XY plane. | Right button
`FRAME` | Control + Right button
`MOVE_FORWARD` | `CAMERA` | Makes the camera go forward at `flySpeed()` and view direction can be changed. |
`MOVE_BACKWARD` | `CAMERA` | Same as `MOVE_FORWARD` but backward. |
`DRIVE` | `CAMERA` | Mouse up/down goes forward/backward proportionally to `flySpeed()` while left/right turns. |
`LOOK_AROUND` | `CAMERA` | Change the viewing direction. The camera position is not modified. |
`ROLL` | `CAMERA` | Rolls camera according to horizontal mouse displacement. | Left and middle buttons (fly mode only)
`SCREEN_ROTATE` | `CAMERA` | Rotates around an axis orthogonal to the screen. | Left and middle buttons
`FRAME` | Control + Left and middle buttons
`SCREEN_TRANSLATE` | `CAMERA` | Translates purely horizontally or vertically wrt screen. | Middle and right buttons
`FRAME` | Control + Middle and right buttons
`ZOOM_ON_REGION` | `CAMERA` | Draws a rectangular region on screen and zooms on it. | Shift + Middle button

The `CAMERA` and `FRAME` default bindings are essentially identical: You simply have to press the `Control` keyboard modifier to move the `FRAME` instead of the `CAMERA`. This modifier key can be modified using `setHandlerKeyboardModifiers()`.

Also note that a double click basically automates the normal mouse action.

`MOVE_FORWARD`, `MOVE_BACKWARD`, `LOOK_AROUND` and `ROLL` are specific to the `CAMERA` fly mode. Press `Space` to switch between revolve and fly camera modes.

Try the simpleViewer and manipulatedFrame examples for an illustration.

## Defining new mouse bindings

If you want to implement a new mouse behavior, simply overload the `mousePressEvent(), mouseMoveEvent(), mouseDoubleClickEvent()` and `mouseReleaseEvent()` callback methods in your `QGLViewer` derived class. See the `QGLViewer::mouseMoveEvent()` documentation for details.

Use `QGLViewer::setMouseBindingDescription()` to add an entry in the help window `Mouse` tab that describes your new mouse binding.

See the keyboardAndMouse example for a practical implementation.

If you implemented a new mouse behavior and you think it can be useful for other applications, send me an e-mail and I will add it in the standard list.

Last modified on Monday, August 31, 2009.