How to get the key code in a vtk KeyPressEvent using python

谁说我不能喝 提交于 2020-01-01 07:32:47

问题


I'm getting started with vtk (6) in python and have a problem handling KeyPressEvent. I want to subclass the vtkInteractorStyleTrackballCamera

if I use this pattern, my interactor style does not have getKeySym() and I can't decode what key was pressed

    class KeyPressInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):

    def __init__(self,parent=None):
        self.AddObserver("MiddleButtonPressEvent",self.middleButtonPressEvent)
        self.AddObserver("MiddleButtonReleaseEvent",self.middleButtonReleaseEvent)
        self.AddObserver("KeyPressEvent",self.keyPress)

    def keyPress(self,obj,event):
        key = obj.GetKeySym() #Does not work
        print("key %s" % key)
        return

    def middleButtonPressEvent(self,obj,event):
        ...
        return

    def middleButtonReleaseEvent(self,obj,event):
        ...
        return

However if I use the factory vtkInteractorStyleTrackballCamera class and add observers with this pattern, the same keyPress() is able to access GetKeySym().

def KeyPress(obj,event):
    key = obj.GetKeySym() #works fine
    print("key %s" % key)

def MiddleButtonPressEvent(obj,event):
    ...
    return

def MiddleButtonReleaseEvent(obj,event):
    ...
    return
...
interactor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
    interactor.AddObserver("KeyPressEvent",KeyPress)
    interactor.AddObserver("MiddleButtonPressEvent",MiddleButtonPressEvent)
    interactor.AddObserver("MiddleButtonReleaseEvent",MiddleButtonReleaseEvent)

I'm a newbie, what should I do to get functionality in my class?


回答1:


If you are making an Interactor Style class I think it's safe to assume it will be applied to an interactor as some point. I noticed in your solution you set the parent to be a vtk.vtkRenderWindowInteractor(). It would be better to set the parent to be the specific instance of vtkRenderWindowInteractor:

class MyInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):

    def __init__(self,parent=None):
        self.parent = iren

        self.AddObserver("KeyPressEvent",self.keyPressEvent)

    def keyPressEvent(self,obj,event):
        key = self.parent.GetKeySym()
        if key == 'l':
            print(key)
        return

...

iren = vtk.vtkRenderWindowInteractor()

iren.SetInteractorStyle(MyInteractorStyle())

iren.SetRenderWindow(renWin)
renWin.Render()

iren.Initialize()
iren.Start()



回答2:


Right I found a solution after checking the vtk doxygen for and seeing that vtkInteractorStyleTrackballCamera does not inherit from vtkRenderWindowInteractor which I had assumed in the example I was porting. I decided to pass my style class a parent so that it could access the RenderWindow GetKeySym(). It may not be the best solution, but here's how as an FYI:

class KeyPressInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):

    def __init__(self,parent=None):
        self.parent = vtk.vtkRenderWindowInteractor()
        if(parent is not None):
            self.parent = parent

        self.AddObserver("KeyPressEvent",self.keyPress)

    def keyPress(self,obj,event):
        key = self.parent.GetKeySym()
...
    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetInteractorStyle(KeyPressInteractorStyle(parent = interactor))



回答3:


I found this question by googling "vtk keypressevent python" and just wanted to share my solution as there is no need for subclassing vtkInteractorStyle:

def keypress_callback(obj, ev):
    key = obj.GetKeySym()
    print(key, 'was pressed')

render_interactor = vtk.vtkRenderWindowInteractor()
render_interactor.AddObserver('KeyPressEvent', keypress_callback, 1.0)


来源:https://stackoverflow.com/questions/32636503/how-to-get-the-key-code-in-a-vtk-keypressevent-using-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!