# How to use Press Key events with screenshot

**URL:** https://discourse.vtk.org/t/how-to-use-press-key-events-with-screenshot/4656
**Category:** Support
**Created:** [November 13, 2020, 2:50pm UTC](https://discourse.vtk.org/t/how-to-use-press-key-events-with-screenshot/4656 "2020-11-13T14:50:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Alaa\_Eddine\_MAZOUZ](https://discourse.vtk.org/user_avatar/discourse.vtk.org/alaa_eddine_mazouz/32/2537_2.png) [@Alaa\_Eddine\_MAZOUZ](https://discourse.vtk.org/u/Alaa_Eddine_MAZOUZ)
#### Post date: [November 13, 2020, 2:50pm UTC](https://discourse.vtk.org/t/how-to-use-press-key-events-with-screenshot/4656/1 "2020-11-13T14:50:29Z")

</div>

Hello, I’m new to VTK and have been trying to work on a small project in c++ to familiarize my self.

I create an actor in a scene and I want to take a screenshot of the window when I want using a press key event, I followed some examples and ended up using a .h file with an observer and an observer function in the main file.

**Here is the header file, please ignore the whole resolution thing, it’s just for testing and it does work fine, the screenshot however does not.**  
#include \<vtkCommand.h\>  
#include\<vtkConeSource.h\>  
#include \<vtkPNGWriter.h\>  
#include \<vtkWindowToImageFilter.h\>  
class Observer : public vtkCommand {  
public:  
static Observer\* New() { return new Observer; }  
void Execute(vtkObject\*, unsigned long, void\*);  
void setConeSource(vtkConeSource \*coneSource) {  
this-\>ConeSource = coneSource;  
resolution = coneSource-\>GetResolution();  
radius= coneSource-\>GetRadius();  
up = true;  
}

```
void windowOb(vtkWindow *window) {
	vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
		vtkSmartPointer<vtkWindowToImageFilter>::New();
	vtkSmartPointer<vtkPNGWriter> pngWriter =
		vtkSmartPointer<vtkPNGWriter>::New();
	this->Window = window;
	window->Render();
	if (shot = true) {
		windowToImageFilter->SetInput(window);
		windowToImageFilter->SetInputBufferTypeToRGBA(); //also record the alpha (transparency) channel
		windowToImageFilter->ReadFrontBufferOff(); // read from the back buffer
		windowToImageFilter->Update();
		windowToImageFilter->GetOutputPort();
		pngWriter->SetFileName("screenshot.png");
		pngWriter->SetInputConnection(windowToImageFilter->GetOutputPort());
		pngWriter->Write();
		Window->Render();
	}
}

void SetResolutionMin(int res) { this->resolutionMin = res; }
void SetResolutionMax(int res) { this->resolutionMax = res; }
void SetRadiusMin(int radius) { this->radMin = radius; }
void SetRadiusMax(int radius) { this->radMax = radius; }

```

private:  
vtkConeSource \*ConeSource;  
//vtkWindowToImageFilter \*WindowToImageFilter;  
//vtkPNGWriter \*PNGWriter;  
vtkWindow \*Window;

```
int resolution,resolutionMin,resolutionMax, radius, radMax, radMin;
bool up, shot;

```

} ;

**Here is the function on the main cpp, again while testing the observer I tested resolution changing which works, not sure how to add screenshot to it though, since I want to take a screenshot everytime I want using a key press, what I’m getting now is one screenshot saved as soon as I run the code and that’s all.**

void Observer::Execute(vtkObject _caller, unsigned long eventID, void_) {  
vtkRenderWindowInteractor \*interactor = vtkRenderWindowInteractor::SafeDownCast(caller);  
if (interactor-\>GetKeyCode() == ‘s’) {  
shot = true;  
cout \<\< “s key pressed” \<\< endl;  
}  
//R pressed?  
if (interactor-\>GetKeyCode() == ‘r’) {

```
	//Changer resolution of cone
	cout << "r key pressed"<<endl;
	if (resolution > resolutionMax) up = false;
	else if (resolution > resolutionMax) up = true;
	
	//resolution++;
	if (up) resolution++, radius++;
	else resolution--, radius--;
	ConeSource->SetResolution(resolution);
	ConeSource->SetRadius(radius);
}

```

}

**Here is the last part of the code where I cann the observer**

vtkSmartPointer observer = vtkSmartPointer::New();  
observer-\>setConeSource(coneSource);  
observer-\>windowOb(window);  
renderer-\>ResetCamera();  
observer-\>SetResolutionMax(50);  
observer-\>SetResolutionMin(4);  
observer-\>SetRadiusMin(1);  
observer-\>SetRadiusMax(10);  
interactor-\>AddObserver(vtkCommand::KeyPressEvent, observer);  
window-\>Render();  
interactor-\>Start();

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [November 16, 2020, 1:37pm UTC](https://discourse.vtk.org/t/how-to-use-press-key-events-with-screenshot/4656/2 "2020-11-16T13:37:34Z")

</div>

Hi, friend,

```
Can you point us exacly the lines of code that is supposed to save the screen shot? Or would you like a code to do it?

```

cheers,

Paulo

---

<div class="post-metadata">

### Author: ![Alaa\_Eddine\_MAZOUZ](https://discourse.vtk.org/user_avatar/discourse.vtk.org/alaa_eddine_mazouz/32/2537_2.png) [@Alaa\_Eddine\_MAZOUZ](https://discourse.vtk.org/u/Alaa_Eddine_MAZOUZ)
#### Post date: [November 16, 2020, 5:42pm UTC](https://discourse.vtk.org/t/how-to-use-press-key-events-with-screenshot/4656/3 "2020-11-16T17:42:29Z")

</div>

I believe I found the solution, I had to move the block of code responsible for writing the data into a file outside the header file and into the observer function in the main file, and use the observer caller to just pass the window data to the header.
