# Hierarchical Mouse Events

**URL:** https://discourse.vtk.org/t/hierarchical-mouse-events/10189
**Category:** Support
**Tags:** python
**Created:** [December 14, 2022, 12:34pm UTC](https://discourse.vtk.org/t/hierarchical-mouse-events/10189 "2022-12-14T12:34:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![llobera](https://discourse.vtk.org/user_avatar/discourse.vtk.org/llobera/32/5540_2.png) [@llobera](https://discourse.vtk.org/u/llobera)
#### Post date: [December 14, 2022, 12:34pm UTC](https://discourse.vtk.org/t/hierarchical-mouse-events/10189/1 "2022-12-14T12:34:38Z")

</div>

This is probably a simple question but I have not been able to figure it out.

I want to be able to implement implement a hierarchical processing of mouse events. For instance, I would like to be able to press on the middle mouse button and then and only then rotate the camera around its position.

Can anyone indicate how to achieve this or point towards some example?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [December 20, 2022, 1:17pm UTC](https://discourse.vtk.org/t/hierarchical-mouse-events/10189/2 "2022-12-20T13:17:46Z")

</div>

Hello,

The usual behavior for dragging with the middle button is to pan the scene. Do you want to change that behavior? What do you mean by “hierarchical”?

best,

Paulo

---

<div class="post-metadata">

### Author: ![llobera](https://discourse.vtk.org/user_avatar/discourse.vtk.org/llobera/32/5540_2.png) [@llobera](https://discourse.vtk.org/u/llobera)
#### Post date: [December 20, 2022, 1:50pm UTC](https://discourse.vtk.org/t/hierarchical-mouse-events/10189/3 "2022-12-20T13:50:04Z")

</div>

Thanks Paulo,

I want to be able to change this behavior. As I mentioned, I would like to have certain camera behaviors that are activated only after certain buttons are pressed. For example, I would like to click the middle mouse button and then and only then have mouse movements to change the yaw and pitch of the camera. If I release the middle button or click again, movements of the mouse trigger movements in the camera position… and so on. So how the movement of the mouse is ‘interpreted’ will depend on whether the middle button was pressed or not. Hopefully this clarifies a bit more what I am after.

thanks,

M

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [December 20, 2022, 2:20pm UTC](https://discourse.vtk.org/t/hierarchical-mouse-events/10189/4 "2022-12-20T14:20:19Z")

</div>

Hello,

To achieve that, you likely need to override certain methods of `vtkInteractorStyleTrackballCamera` class. I do something like you need by setting boolean variables:

```cpp
void myMouseInteractor::OnLeftButtonDown()
{
    m_isLBdown = true;

    // Forward the event to the superclass.
    vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}

void myMouseInteractor::OnMouseMove()
{
    if( m_isLBdown )
        m_isDragging = true;

    // Forward the event to the superclass.
    vtkInteractorStyleTrackballCamera::OnMouseMove();
}

void myMouseInteractor::OnLeftButtonUp()
{
    // Don't pick during dragging.
    if( ! m_isDragging ){

        //Remove the marker actor anyway.
        if( m_pickMarkerActor ) {
            this->GetDefaultRenderer()->RemoveActor( m_pickMarkerActor );
            m_pickMarkerActor = nullptr;
        }
        (...)
    }
    m_isLBdown = false;
    m_isDragging = false;
    // Forward the event to the superclass.
    vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
}

```

In the example above, I don’t want to trigger a pick/probing if the user is just rotating the scene. Notice how I had to customize three mouse events to achive that.

Complete code here: [gammaray/v3dmouseinteractor.h at master · PauloCarvalhoRJ/gammaray · GitHub](https://github.com/PauloCarvalhoRJ/gammaray/blob/master/viewer3d/v3dmouseinteractor.h) and [gammaray/v3dmouseinteractor.cpp at master · PauloCarvalhoRJ/gammaray · GitHub](https://github.com/PauloCarvalhoRJ/gammaray/blob/master/viewer3d/v3dmouseinteractor.cpp).

regards,

Paulo

---

<div class="post-metadata">

### Author: ![llobera](https://discourse.vtk.org/user_avatar/discourse.vtk.org/llobera/32/5540_2.png) [@llobera](https://discourse.vtk.org/u/llobera)
#### Post date: [December 20, 2022, 2:25pm UTC](https://discourse.vtk.org/t/hierarchical-mouse-events/10189/5 "2022-12-20T14:25:52Z")

</div>

Thanks Paulo… this is helpful!
