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?
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):
print(event)
if event == "KeyPressEvent":
key = interactor.GetKeySym()
if key.lower() == 'backslash':
xy = interactor.GetEventPosition()
print(xy)
return False
interactorObserverTags = []
events = ( vtk.vtkCommand.LeftButtonPressEvent,
vtk.vtkCommand.LeftButtonReleaseEvent,
vtk.vtkCommand.MiddleButtonPressEvent,
vtk.vtkCommand.MiddleButtonReleaseEvent,
vtk.vtkCommand.RightButtonPressEvent,
vtk.vtkCommand.RightButtonReleaseEvent,
vtk.vtkCommand.MouseMoveEvent,
vtk.vtkCommand.KeyPressEvent,
vtk.vtkCommand.EnterEvent,
vtk.vtkCommand.LeaveEvent )
for e in events:
tag = interactor.AddObserver(e, processEvent, 1.0)
interactorObserverTags.append(tag)