# Capturing Mouse Input

**URL:** <https://discourse.slicer.org/t/capturing-mouse-input/789>\
**Category:** Development\
**Created:** [July 31, 2017, 6:01am UTC](https://discourse.slicer.org/t/capturing-mouse-input/789 "2017-07-31T06:01:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![moselhy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/moselhy/32/501_2.png) [@moselhy](https://discourse.slicer.org/u/moselhy)\
**Post date:** [July 31, 2017, 6:01am UTC](https://discourse.slicer.org/t/capturing-mouse-input/789/1 "2017-07-31T06:01:49Z")

</div>

Hello,

I would like to ask the user to input coordinates by clicking on a point in the volume. How do I achieve this?

I guess I would prompt them using a QMessageBox, but I would also like to change the pointer to something like this

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/b/6b22248c9ecc5b4fc6d8a0f526bfc9b9ceed69d2.jpeg) and actually await the input…

Thanks

---

<div class="post-metadata">

**Author:** ![Davide\_Punzo](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/davide_punzo/32/66104_2.png) [@Davide\_Punzo](https://discourse.slicer.org/u/Davide_Punzo)\
**Post date:** [July 31, 2017, 9:28am UTC](https://discourse.slicer.org/t/capturing-mouse-input/789/2 "2017-07-31T09:28:06Z")

</div>

Hi Mohamed,

you may try to do a segment editor effect. For example, you can take a look at the user-interactions available in the paint effect (works both in 2d and 3d: takes the input coordinates for left-click events) and create your own:

> <https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Segmentations/EditorEffects/qSlicerSegmentEditorPaintEffect.h>

The editor has an effects factory, so you can register your new editor effect very simply, something like this:

> <https://github.com/Punzo/SlicerAstro/blob/master/AstroVolume/qSlicerAstroVolumeModule.cxx#L413>

Cheers,

Davide.

---

<div class="post-metadata">

**Author:** ![Nicole\_Aucoin](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Nicole\_Aucoin](https://discourse.slicer.org/u/Nicole_Aucoin)\
**Post date:** [July 31, 2017, 1:25pm UTC](https://discourse.slicer.org/t/capturing-mouse-input/789/3 "2017-07-31T13:25:01Z")

</div>

You can use the Markups module to go into fiducial placement mode, see  
the “Adding  
Fiducials via Mouse Clicks” section here:  
[https://www.slicer.org/wiki/Documentation/Nightly/Modules/Markups](https://www.slicer.org/wiki/Documentation/Nightly/Modules/Markups)

Nicole

---

<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 31, 2017, 1:55pm UTC](https://discourse.slicer.org/t/capturing-mouse-input/789/4 "2017-07-31T13:55:33Z")

</div>

Modules in VMTK extension uses fiducials as seeds. You can check out how add a widget to your module that can create/select fiducials: [https://github.com/vmtk/SlicerExtension-VMTK/blob/master/CenterlineComputation/CenterlineComputation.py#L74-L86](https://github.com/vmtk/SlicerExtension-VMTK/blob/master/CenterlineComputation/CenterlineComputation.py#L74-L86)

You can tune it to create/select fiducial list by default and only show the place widget with a single button to place widget.

See also these examples:

- [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Switching\_to\_markup\_fiducial\_placement\_mode](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Switching_to_markup_fiducial_placement_mode)
- [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Manipulating\_objects\_in\_the\_slice\_viewer](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Manipulating_objects_in_the_slice_viewer)

---

<div class="post-metadata">

**Author:** ![moselhy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/moselhy/32/501_2.png) [@moselhy](https://discourse.slicer.org/u/moselhy)\
**Post date:** [July 31, 2017, 9:46pm UTC](https://discourse.slicer.org/t/capturing-mouse-input/789/5 "2017-07-31T21:46:51Z")

</div>

When I execute `self.promptSeedSelect("background")`  
It is returning `[0,0,0]`. Is there any way I can force the program to wait for a mouse click when the user clicks ‘Ok’?

```
  def promptSeedSelect(self, seed):
    c = ctk.ctkMessageBox()
    c.setIcon(qt.QMessageBox.Information)
    c.setText("Click on a point in the %s to select the seed" % seed)
    c.setStandardButtons(qt.QMessageBox.Ok)
    c.setDefaultButton(qt.QMessageBox.Ok)
    answer = c.exec_()
    if answer == qt.QMessageBox.Cancel:
      logging.info("Terminating...")
      self.logic = None
      return

    logging.info("Coords are: " + self.drawFiducial(seed))

  def drawFiducial(self, seed):
    ras = [0.0,0.0,0.0]

    fidNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLMarkupsFiducialNode', seed)
    selectionNode = slicer.mrmlScene.GetNodeByID("vtkMRMLSelectionNodeSingleton")
    selectionNode.SetReferenceActivePlaceNodeID(fidNode.GetID())
    interactionNode = slicer.mrmlScene.GetNodeByID("vtkMRMLInteractionNodeSingleton")
    # For multiple clicks, change this to 1
    placeModePersistence = 0
    interactionNode.SetPlaceModePersistence(placeModePersistence)
    interactionNode.SetCurrentInteractionMode(1)

    fidNode.GetNthFiducialPosition(0, ras)

    return ras
```

---

<div class="post-metadata">

**Author:** ![Nicole\_Aucoin](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Nicole\_Aucoin](https://discourse.slicer.org/u/Nicole_Aucoin)\
**Post date:** [July 31, 2017, 10:14pm UTC](https://discourse.slicer.org/t/capturing-mouse-input/789/6 "2017-07-31T22:14:54Z")

</div>

You need to add an observer on the fiducial list node for the  
slicer.vtkMRMLMarkupsNode.MarkupAddedEvent  
and then you can get the last fiducial position since it’s then been added.

Nicole

---

<div class="post-metadata">

**Author:** ![moselhy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/moselhy/32/501_2.png) [@moselhy](https://discourse.slicer.org/u/moselhy)\
**Post date:** [August 4, 2017, 4:58am UTC](https://discourse.slicer.org/t/capturing-mouse-input/789/7 "2017-08-04T04:58:59Z")

</div>

Thank you. When I did that, the code runs fine but then I couldn’t get it to capture the input a second time if the user chooses to. Here is my full code:

> <https://github.com/moselhy/SlicerPhantomSegmenter/blob/master/PhantomSegmenter/PhantomSegmenter.py>

To reproduce my problem, select this extension from Slicer’s Extension Wizard, then add a breakpoint to [line 265](https://github.com/moselhy/PhantomSegmenter/blob/master/PhantomSegmenter/PhantomSegmenter.py#L265) and set a watch with the following:  
slicer.mrmlScene.GetNodeByID(“vtkMRMLInteractionNodeSingleton”).GetCurrentInteractionMode()

You’ll see that it shows 1 in the debugger, which we want, but then when it exits the call stack, it spontaneously switches to 2, which does not allow placement of a fiducial.

---

<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 4, 2017, 5:16am UTC](https://discourse.slicer.org/t/capturing-mouse-input/789/8 "2017-08-04T05:16:05Z")

</div>

To facilitate placement of multiple markups, use [qSlicerMarkupsPlaceWidget.setPlaceMultipleMarkups(ForcePlaceMultipleMarkups)](http://apidocs.slicer.org/master/classqSlicerMarkupsPlaceWidget.html#a4832089c5d68137535a8948bfe6d6e7a).

As I see, you have commented out the markup place widget, while you shouldn’t have, because that widget is exactly what you need (it makes sure that the correct markup list is selected, placement mode is active, and single/multiple markup placement option is selected). Just add this widget to the GUI and modify the place multiple markups mode:

```
self.seedFiducialsNodeSelector.markupsPlaceWidget().placeMultipleMarkups = slicer.qSlicerMarkupsPlaceWidget.ForcePlaceMultipleMarkups
```

---

<div class="post-metadata">

**Author:** ![moselhy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/moselhy/32/501_2.png) [@moselhy](https://discourse.slicer.org/u/moselhy)\
**Post date:** [August 4, 2017, 9:47pm UTC](https://discourse.slicer.org/t/capturing-mouse-input/789/9 "2017-08-04T21:47:18Z")

</div>

Thank you for all your help so far. The reason I commented out the widget is because it would require more manual work from the user. I just want the user to be able to choose whether to add more markups or to stop adding more, using this dialog  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/7/f750be63c45682ab63370bdb31b720696313b3ec.png)

When the user clicks “Add More”, it should create a new fiducial and set an observer to it and show this dialog when the user places a markup, and so on, until the user clicks “Save All”, then it would move to the next step. However, the problem I mentioned above happens when the user clicks “Add More”, and I don’t know why…

---

<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 5, 2017, 2:39am UTC](https://discourse.slicer.org/t/capturing-mouse-input/789/10 "2017-08-05T02:39:18Z")

</div>

Instead of the “Add more” button, use the place widget, configured to show only a single place button, with single markup place mode. It works reliably, used in many modules.

```
w = slicer.qSlicerMarkupsPlaceWidget()
w.buttonsVisible = False
w.placeButton().show()
w.setMRMLScene(slicer.mrmlScene)
w.setCurrentNode(getNode('F'))
w.placeMultipleMarkups = slicer.qSlicerMarkupsPlaceWidget.ForcePlaceSingleMarkup
w.show()

```

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/8/c84741f028d6ecea5299cc0c9825333455533e06.png)
