# Disable interactions of interactor in python

**URL:** https://discourse.vtk.org/t/disable-interactions-of-interactor-in-python/6939
**Category:** Support
**Created:** [October 22, 2021, 11:57am UTC](https://discourse.vtk.org/t/disable-interactions-of-interactor-in-python/6939 "2021-10-22T11:57:09Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![rdbrn](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rdbrn/32/5627_2.png) [@rdbrn](https://discourse.vtk.org/u/rdbrn)
#### Post date: [October 22, 2021, 11:57am UTC](https://discourse.vtk.org/t/disable-interactions-of-interactor-in-python/6939/1 "2021-10-22T11:57:09Z")

</div>

I use vtk in an application (qt) where I would like to define my own events for right-mouse button (context menu) as well as some of the key presses.

Problem is that they are already used by the interactor.  
([https://vtk.org/doc/nightly/html/classvtkInteractorStyle.html#details](https://vtk.org/doc/nightly/html/classvtkInteractorStyle.html#details)) Is there a way to keep the interactor from handling these events?

I’m using python so subclassing the interactor (like sugested here: [Selectively disable and enable default mouse interactions in vtk](https://discourse.vtk.org/t/selectively-disable-and-enable-default-mouse-interactions-in-vtk/2338)) is not an option.

Is there any other way to disable some of the default interactions?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [October 22, 2021, 2:21pm UTC](https://discourse.vtk.org/t/disable-interactions-of-interactor-in-python/6939/2 "2021-10-22T14:21:05Z")

</div>

You can override the interaction in the vtkInteractorStyle classes.

First, choose a vtkInteractorStyle that most closely matches your desired behavior, e.g. vtkInteractorStyleTrackball or vtkInteractorStyleUser.

Then, add Python observers to the events you want to override or disable:

```
style.AddObserver("KeyPressEvent", callback)
style.AddObserver("KeyReleaseEvent", callback)
style.AddObserver("CharEvent", callback)
```

---

<div class="post-metadata">

### Author: ![rdbrn](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rdbrn/32/5627_2.png) [@rdbrn](https://discourse.vtk.org/u/rdbrn)
#### Post date: [October 24, 2021, 12:53pm UTC](https://discourse.vtk.org/t/disable-interactions-of-interactor-in-python/6939/3 "2021-10-24T12:53:49Z")

</div>

Hi @dgobbi , yes that’s what I thought I was doing but it didn’t work.

I now realize that I need to add the oberservers to the STYLE and not to the renderWindowInteractor (which is what I was doing).

Thanks!

Posting a full example here for the next person running into this:

vtkInteractorStyleTrackballCamera with keys and right button features disabled.

```python
import vtk
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersSources import vtkCylinderSource
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)

def main():
    colors = vtkNamedColors()

    # Create a sphere
    cylinderSource = vtkCylinderSource()
    cylinderSource.SetCenter(0.0, 0.0, 0.0)
    cylinderSource.SetRadius(5.0)
    cylinderSource.SetHeight(7.0)
    cylinderSource.SetResolution(100)

    # Create a mapper and actor
    mapper = vtkPolyDataMapper()
    mapper.SetInputConnection(cylinderSource.GetOutputPort())
    actor = vtkActor()
    actor.GetProperty().SetColor(colors.GetColor3d('Cornsilk'))
    actor.SetMapper(mapper)

    # Create a renderer, render window, and interactor
    renderer = vtkRenderer()
    renderWindow = vtkRenderWindow()
    renderWindow.SetWindowName('Cylinder')
    renderWindow.AddRenderer(renderer)

    style = vtk.vtkInteractorStyleTrackballCamera()

    style.AddObserver("CharEvent", lambda x, y: print('key char'))
    style.AddObserver("RightButtonPressEvent", lambda x, y: print('right press'))
    style.AddObserver("RightButtonReleaseEvent", lambda x, y: print('right release'))

    renderWindowInteractor = vtkRenderWindowInteractor()
    renderWindowInteractor.SetInteractorStyle(style)
    renderWindowInteractor.SetRenderWindow(renderWindow)

    # Add the actor to the scene
    renderer.AddActor(actor)
    renderer.SetBackground(colors.GetColor3d('DarkGreen'))

    # Render and interact
    renderWindow.Render()
    renderWindowInteractor.Start()

if __name__ == ' __main__':
    main()

```
