# Exporting images by key operation.

**URL:** <https://discourse.vtk.org/t/exporting-images-by-key-operation/1756>\
**Category:** Development\
**Created:** [September 13, 2019, 9:00am UTC](https://discourse.vtk.org/t/exporting-images-by-key-operation/1756 "2019-09-13T09:00:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![1111](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/1/71e660/32.png) [@1111](https://discourse.vtk.org/u/1111)\
**Post date:** [September 13, 2019, 9:00am UTC](https://discourse.vtk.org/t/exporting-images-by-key-operation/1756/1 "2019-09-13T09:00:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![1111](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/1/71e660/32.png) [@1111](https://discourse.vtk.org/u/1111)\
**Post date:** [September 17, 2019, 10:14am UTC](https://discourse.vtk.org/t/exporting-images-by-key-operation/1756/2 "2019-09-17T10:14:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [September 18, 2019, 3:12pm UTC](https://discourse.vtk.org/t/exporting-images-by-key-operation/1756/3 "2019-09-18T15:12:19Z")

</div>

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/KeypressObserver/)  
[https://lorensen.github.io/VTKExamples/site/Cxx/Interaction/KeypressEvents/](https://lorensen.github.io/VTKExamples/site/Cxx/Interaction/KeypressEvents/)

---

<div class="post-metadata">

**Author:** ![1111](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/1/71e660/32.png) [@1111](https://discourse.vtk.org/u/1111)\
**Post date:** [September 19, 2019, 4:26am UTC](https://discourse.vtk.org/t/exporting-images-by-key-operation/1756/4 "2019-09-19T04:26:10Z")

</div>

Hi Gobbi,  
Thank you for your usual reply.

I will create a program with reference to the sample.

---

<div class="post-metadata">

**Author:** ![1111](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/1/71e660/32.png) [@1111](https://discourse.vtk.org/u/1111)\
**Post date:** [September 24, 2019, 10:40am UTC](https://discourse.vtk.org/t/exporting-images-by-key-operation/1756/5 "2019-09-24T10:40:33Z")

</div>

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
