Windows上VTK中的箭头键事件

时间:2015-10-13 17:12:57

标签: python windows keyboard keyboard-events vtk

为什么箭头键按事件不会转发到Windows上的vtkRenderWindowInteractor?有解决方法吗? Windows和Mac上的箭头键事件之间是否存在一般差异?

我可以使用以下示例代码重现该问题。在Mac OS上,如果按箭头键,我会看到“向上”,“向下”,“向左”和“向右”。但在Windows上,我没有看到任何内容(没有输入回调)。我想知道为什么会这样。

我使用VTK 6.2.0 for python 2.7。我在Windows Server 2012(类似于Windows 8)和Windows 7(在虚拟机上)上进行了测试,显示了相同的行为。

# -*- coding: utf-8 -*-
import vtk
import sys

class ClickInteractorStyle(vtk.vtkInteractorStyleTrackballCamera):
    def __init__(self, parent=None):
            self.AddObserver("CharEvent",self.onKeyPressEvent)

    def onKeyPressEvent(self, renderer, event):        
        key = self.GetInteractor().GetKeySym()
        print key
        self.OnChar()

def run():
    sphereSource = vtk.vtkSphereSource()
    sphereSource.SetCenter(0.,0.,0.)
    sphereSource.SetRadius(1.)
    sphereSource.Update()

    sphereMapper = vtk.vtkPolyDataMapper()
    sphereMapper.SetInputConnection(sphereSource.GetOutputPort())

    sphereActor = vtk.vtkActor()
    sphereActor.SetMapper(sphereMapper)

    renderer = vtk.vtkRenderer()
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.AddRenderer(renderer)

    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)
    style = ClickInteractorStyle()
    renderWindowInteractor.SetInteractorStyle(style)
    style.SetCurrentRenderer(renderer)

    renderer.AddActor(sphereActor)
    renderWindow.Render()
    renderWindowInteractor.Start()



###############################################################################
# MAIN
###############################################################################
if __name__ == "__main__":
    try:
        run()
    except:
        print "Unexpected error: %s", sys.exc_info()[0]
        raise

1 个答案:

答案 0 :(得分:1)

这解决了我的问题:替换

self.AddObserver("CharEvent",self.onKeyPressEvent)

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