# Entry and exit events from MRML slice views

**URL:** <https://discourse.slicer.org/t/entry-and-exit-events-from-mrml-slice-views/1060>\
**Category:** Development\
**Created:** [September 15, 2017, 3:34pm UTC](https://discourse.slicer.org/t/entry-and-exit-events-from-mrml-slice-views/1060 "2017-09-15T15:34:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschumaker](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mschumaker/32/1395_2.png) [@mschumaker](https://discourse.slicer.org/u/mschumaker)\
**Post date:** [September 15, 2017, 3:34pm UTC](https://discourse.slicer.org/t/entry-and-exit-events-from-mrml-slice-views/1060/1 "2017-09-15T15:34:56Z")

</div>

I’m hoping to perform actions in my scripted module when the user’s mouse enters or exits the Red and Yellow MRML slice views. I know this involves observing events, but I’m not sure what event to observe, and where to use AddObserver. How can I accomplish this?  
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:** [September 15, 2017, 4:00pm UTC](https://discourse.slicer.org/t/entry-and-exit-events-from-mrml-slice-views/1060/2 "2017-09-15T16:00:20Z")

</div>

These are standard VTK interaction events that you can get by observing low-level VTK objects (probably the interactor; see examples of accessing these low-level objects in the [script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Accessing_views.2C_renderers.2C_and_cameras)).

However, I would recommend to use higher-level API, such as [observing the crosshair node](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_current_mouse_coordinates_in_a_slice_view). You can get the current slice node using [vtkMRMLCrosshairNode::GetCursorPositionXYZ](http://apidocs.slicer.org/master/classvtkMRMLCrosshairNode.html#adab412c60e3be57eb5f6612d96667e96) method.

---

<div class="post-metadata">

**Author:** ![mschumaker](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mschumaker/32/1395_2.png) [@mschumaker](https://discourse.slicer.org/u/mschumaker)\
**Post date:** [September 15, 2017, 5:28pm UTC](https://discourse.slicer.org/t/entry-and-exit-events-from-mrml-slice-views/1060/3 "2017-09-15T17:28:18Z")

</div>

Excellent, thank you. I’m able to get the slice node and its name that way.  
I am using a ctkWorkflow, and I’ve also set it up to add the observer when advancing to the relevant step, and remove the observer afterwards:

```
def onEntry(self):
    ...
    self.crosshairNode=slicer.util.getNode('Crosshair')
    self.crosshairNodeObserverTag = self.crosshairNode.AddObserver(slicer.vtkMRMLCrosshairNode.CursorPositionModifiedEvent, self.onMouseMoved)
#end onEntry

def onExit(self):
    if(self.crosshairNodeObserverTag is not None):
        self.crosshairNode.RemoveObserver(self.crosshairNodeObserverTag)
#end onExit
```

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [July 26, 2019, 4:25am UTC](https://discourse.slicer.org/t/entry-and-exit-events-from-mrml-slice-views/1060/4 "2019-07-26T04:25:23Z")

</div>

I am creating a crosshairnode but still the program shows erroe crosshairnode not defined

```python
self.crosshairNode=slicer.mrmlScene.AddNewNodeByClass("vtkMRMLCrosshairNode")
self.observeid = self.crosshairNode.AddObserver(slicer.vtkMRMLCrosshairNode.CursorPositionModifiedEvent, self.onMouseMoved)

```

---

<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:** [July 26, 2019, 12:33pm UTC](https://discourse.slicer.org/t/entry-and-exit-events-from-mrml-slice-views/1060/5 "2019-07-26T12:33:13Z")

</div>

There is a singleton crosshair node, which is used by the application. You need to retrieve and use that node, as shown in the examples (do not create a new crosshair node).
