# KeypressEvents

VTKExamples/Cxx/Interaction/KeypressEvents

### Description

This example shows how to catch keypress events. For example, do something if the user presses the 's' key.

### Code

KeypressEvents.cxx

    #include <vtkPolyDataMapper.h>
    #include <vtkObjectFactory.h>
    #include <vtkActor.h>
    #include <vtkSmartPointer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkPolyData.h>
    #include <vtkSphereSource.h>
    #include <vtkInteractorStyleTrackballCamera.h>

    // Define interaction style
    class KeyPressInteractorStyle : public vtkInteractorStyleTrackballCamera
    {
      public:
        static KeyPressInteractorStyle* New();
        vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyleTrackballCamera);

        virtual void OnKeyPress()
        {
          // Get the keypress
          vtkRenderWindowInteractor *rwi = this->Interactor;
          std::string key = rwi->GetKeySym();

          // Output the key that was pressed
          std::cout << "Pressed " << key << std::endl;

          // Handle an arrow key
          if(key == "Up")
          {
            std::cout << "The up arrow was pressed." << std::endl;
          }

          // Handle a "normal" key
          if(key == "a")
          {
            std::cout << "The a key was pressed." << std::endl;
          }

          // Forward events
          vtkInteractorStyleTrackballCamera::OnKeyPress();
        }

    };
    vtkStandardNewMacro(KeyPressInteractorStyle);

    int main(int, char *[])
    {
      vtkSmartPointer<vtkSphereSource> sphereSource =
        vtkSmartPointer<vtkSphereSource>::New();
      sphereSource->SetCenter(0.0, 0.0, 0.0);
      sphereSource->SetRadius(5.0);
      sphereSource->Update();

      vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      mapper->SetInputConnection(sphereSource->GetOutputPort());

      // Create an actor
      vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
      actor->SetMapper(mapper);

      // A renderer and render window
      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);

      // An interactor
      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow(renderWindow);

      vtkSmartPointer<KeyPressInteractorStyle> style =
        vtkSmartPointer<KeyPressInteractorStyle>::New();
      renderWindowInteractor->SetInteractorStyle(style);
      style->SetCurrentRenderer(renderer);

      renderer->AddActor(actor);
      renderer->SetBackground(1,1,1); // Background color white

      renderWindow->Render();

      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    PROJECT(KeypressEvents)

    find_package(VTK REQUIRED)
    include(${VTK_USE_FILE})

    add_executable(KeypressEvents MACOSX_BUNDLE KeypressEvents.cxx )

    target_link_libraries(KeypressEvents ${VTK_LIBRARIES})

### Download and Build KeypressEvents

Click here to download KeypressEvents and its CMakeLists.txt file. Once the tarball KeypressEvents.tar has been downloaded and extracted,

    cd KeypressEvents/build

If VTK is installed:

    cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

    cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

    make

and run it:

    ./KeypressEvents

WINDOWS USERS PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.