I would like to implement custom functionality based on user clicks/keypress in the slice views. I would like to observe the events separately for clicks/keypress in the red and yellow slices. For this, I was implementing the following code. However, the function does not seem to be called when I press the left mouse button or press a key. Could you please let me know the reason. Is there a better way to independently detect key press events on the red and yellow slices?
def testMethod(caller, event):
color = "Red"
sliceNode = slicer.mrmlScene.GetNodeByID("vtkMRMLSliceNode%s" % color)
It is better to avoid this kind of low-level access and use widgets (markups, ROI, etc.), segment editor effects, etc. What would you like to achieve (define input points, move objects, …)?
If you want to process these internal events anyway then you need to observe events of the interactor:
interactor = slicer.app.layoutManager().sliceWidget('Red').sliceView().interactor()
def processEvent(caller=None, event=None):
if event == "KeyPressEvent":
key = interactor.GetKeySym()
if key.lower() == 'backslash':
xy = interactor.GetEventPosition()
interactorObserverTags = 
events = ( vtk.vtkCommand.LeftButtonPressEvent,
for e in events:
tag = interactor.AddObserver(e, processEvent, 1.0)
Thanks very much, Andras. This works well.