简体   繁体   English

Windows上VTK中的箭头键事件

[英]Arrow-key events in VTK on Windows

Why are arrow key-press events not forwarded to the vtkRenderWindowInteractor on Windows? 为什么箭头按键事件没有转发到Windows上的vtkRenderWindowInteractor? Is there a workaround? 有解决方法吗? Is there a general difference between arrow-key events on Windows and Mac? Windows和Mac上的箭头键事件之间是否有一般区别?

I can reproduce the problem with the following sample code. 我可以使用以下示例代码重现该问题。 On Mac OS, I see 'Up', 'Down', 'Left' and 'Right' if I press the arrow keys. 在Mac OS上,如果按箭头键,则会看到“上”,“下”,“左”和“右”。 But on Windows, I don't see anything (the callback is not entered). 但是在Windows上,我什么也看不到(未输入回调)。 I wondered why this is the case. 我想知道为什么会这样。

I use VTK 6.2.0 for python 2.7. 我将VTK 6.2.0用于python 2.7。 I tested on Windows Server 2012 (similar to Windows 8) and Windows 7 (on a virtual machine), showing both the same behaviour. 我在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

This fixed my problem: Replace 这解决了我的问题:替换

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

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM