# Mouse click over slice

**URL:** <https://discourse.slicer.org/t/mouse-click-over-slice/24967>\
**Category:** Development\
**Created:** [August 28, 2022, 9:33pm UTC](https://discourse.slicer.org/t/mouse-click-over-slice/24967 "2022-08-28T21:33:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JBeninca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jbeninca/32/10039_2.png) [@JBeninca](https://discourse.slicer.org/u/JBeninca)\
**Post date:** [August 28, 2022, 9:33pm UTC](https://discourse.slicer.org/t/mouse-click-over-slice/24967/1 "2022-08-28T21:33:49Z")

</div>

Hi everyone.  
Just a question: how can I read the mouse event (right click) over a widget and then run a call back?  
Is there some function that do it?  
thanks

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 29, 2022, 2:59am UTC](https://discourse.slicer.org/t/mouse-click-over-slice/24967/2 "2022-08-29T02:59:27Z")

</div>

By default, in markup widgets `RightButtonClickEvent` interaction event is translated to `WidgetEventMenu` widget event, which displays a context menu. You can remove the old mapping and add a new mapping to a custom event using `widget.SetEventTranslation` (see examples in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html)).

For example, you can translate the right-click event to `slicer.vtkSlicerMarkupsWidget.WidgetEventCustomAction1` event, which invokes a `slicer.vtkMRMLMarkupsDisplayNode.CustomActionEvent1` on the markup’s display node. To get notified about the right-click event you just need to add an observer to this event.

Example:

```python
def someCustomAction(caller, eventId):
  markupsDisplayNode = caller
  print(f"Custom action activated in {markupsDisplayNode.GetName()}")

views = [
    slicer.app.layoutManager().threeDWidget(0).threeDView(),
    slicer.app.layoutManager().sliceWidget("Red").sliceView(),
    slicer.app.layoutManager().sliceWidget("Yellow").sliceView(),
    slicer.app.layoutManager().sliceWidget("Green").sliceView()
]

observations = [] # store the observations so that later can be removed
markupsDisplayNodes = slicer.util.getNodesByClass("vtkMRMLMarkupsDisplayNode")
for markupsDisplayNode in markupsDisplayNodes:
    # Add observer to custom actions
    observations.append([markupsDisplayNode, markupsDisplayNode.AddObserver(markupsDisplayNode.CustomActionEvent1, someCustomAction)])
    for view in views:
        markupsDisplayableManager = view.displayableManagerByClassName('vtkMRMLMarkupsDisplayableManager')
        # Assign keyboard shortcut to trigger custom actions
        widget = markupsDisplayableManager.GetWidget(markupsDisplayNode)
        # Remove old event translation
        widget.SetEventTranslation(widget.WidgetStateOnWidget, slicer.vtkMRMLInteractionEventData.RightButtonClickEvent, vtk.vtkEvent.NoModifier, vtk.vtkWidgetEvent.NoEvent)
        # Add new event translation
        widget.SetEventTranslation(widget.WidgetStateOnWidget, slicer.vtkMRMLInteractionEventData.RightButtonClickEvent, vtk.vtkEvent.NoModifier, widget.WidgetEventCustomAction1)

## Remove observations when custom actions are not needed anymore by uncommenting these lines:
# for observedNode, observation in observations:
# observedNode.RemoveObserver(observation)

```

---

<div class="post-metadata">

**Author:** ![jay1987](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jay1987/32/16183_2.png) [@jay1987](https://discourse.slicer.org/u/jay1987)\
**Post date:** [August 31, 2022, 2:24am UTC](https://discourse.slicer.org/t/mouse-click-over-slice/24967/3 "2022-08-31T02:24:14Z")

</div>

which widget do you want to react the right click event?  
in slicer,it different to react and to hand right click event with different widgets

---

<div class="post-metadata">

**Author:** ![JBeninca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jbeninca/32/10039_2.png) [@JBeninca](https://discourse.slicer.org/u/JBeninca)\
**Post date:** [August 31, 2022, 9:17am UTC](https://discourse.slicer.org/t/mouse-click-over-slice/24967/4 "2022-08-31T09:17:07Z")

</div>

Hi. It’s the Red widget.  
Else, i want to tell you, that its ready a first version of a Leksell path stereotaxia.  
It does works fine, but it need some tests. If you hace 2 or 3 different heads-with-frame volumen, please send them to me.

---

<div class="post-metadata">

**Author:** ![Grigoriy\_Postolskiy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/grigoriy_postolskiy/32/15587_2.png) [@Grigoriy\_Postolskiy](https://discourse.slicer.org/u/Grigoriy_Postolskiy)\
**Post date:** [May 26, 2023, 9:38am UTC](https://discourse.slicer.org/t/mouse-click-over-slice/24967/5 "2023-05-26T09:38:46Z")

</div>

This code started work for only when I added the decorator above the function:

```auto
@vtk.calldata_type(vtk.VTK_OBJECT)
def someCustomAction(caller, eventId, callData):

```
