Exporting images by key operation.

Hi,
Currently, I am developing imaging using vtkImplicitPlaneWidget2.

I’m thinking about saving to my file by pressing a certain key on the window.
First, identify the keys and create code to operate them.

Can you give me some tips on how to make certain keys work?
Also, can you tell me if there are any good examples?

For example, when you press the “w” key on the vtkImplicitPlaneWidget2 window, you want to save the slice plane image as a two-dimensional image (.raw).

I don’t know how to make it work by pressing a key.
If you know how to do it, can you give me some tips on how to solve it?

@dgobbi

Here are examples of two different ways to get keypress events:

https://lorensen.github.io/VTKExamples/site/Cxx/Interaction/KeypressObserver/
https://lorensen.github.io/VTKExamples/site/Cxx/Interaction/KeypressEvents/

Hi Gobbi,
Thank you for your usual reply.

I will create a program with reference to the sample.

Hi Gobbi,
I am creating a program to save raw data of slice plane by pressing “c” based on KeypressEvents.

The program of key press partunder construction is shown below:

  class KeyPressInteractorStyle : public vtkInteractorStyleTrackballCamera
{
 public:
 static KeyPressInteractorStyle* New();
 vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyleTrackballCamera);

virtual void OnKeyPress() override
{
  // 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 a "normal" key
  if(key == "c")
  {
     std::cout << "The a key was pressed." << std::endl;
  
  std::string filePathRaw = "reslice_512x512x512.raw";

 vtkSmartPointer<vtkImageCast> castFilter = 
  vtkSmartPointer<vtkImageCast>::New();
 castFilter->SetOutputScalarTypeToUnsignedChar();
    castFilter->SetInputConnection(reslice->GetOutputPort());
    castFilter->Update();

 vtkSmartPointer<vtkImageWriter> writer =
  vtkSmartPointer<vtkImageWriter>::New();
   writer->SetInputConnection(castFilter->GetOutputPort());
   writer->SetFileName(filePathRaw.c_str());
   writer->Write();
}
  // Forward events
  vtkInteractorStyleTrackballCamera::OnKeyPress();
  }
};
vtkStandardNewMacro(KeyPressInteractorStyle);

I don’t know how to adapt the updated plane to the castFilter’s SetInputConnection by callback.

 vtkSmartPointer<vtkImageCast> castFilter = 
  vtkSmartPointer<vtkImageCast>::New();
 castFilter->SetOutputScalarTypeToUnsignedChar();
 castFilter->SetInputConnection(reader1->GetOutputPort());
 castFilter->Update();

Could you advise how to apply the plane information to the key press part?

@dgobbi