# Callback keypress interaction not working

**URL:** https://discourse.vtk.org/t/callback-keypress-interaction-not-working/1865
**Category:** Support
**Created:** [October 4, 2019, 11:01am UTC](https://discourse.vtk.org/t/callback-keypress-interaction-not-working/1865 "2019-10-04T11:01:59Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Mikend994](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@Mikend994](https://discourse.vtk.org/u/Mikend994)
#### Post date: [October 4, 2019, 11:01am UTC](https://discourse.vtk.org/t/callback-keypress-interaction-not-working/1865/1 "2019-10-04T11:01:59Z")

</div>

Hi, I’m trying to use a callback function for starting an action when a key(keyboard) is pressed. For doing this I’m implementing the “KeyPressObserver” example. My target is to increase the sphere radius when I click on “Right” arrow. This is the code

```
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>
#include <vtkCallbackCommand.h>
#include <vtkCommand.h>
#include <vtkInteractorStyleTrackballCamera.h>

void KeypressCallbackFunction(
vtkObject* caller,
long unsigned int eventId,
void* clientData,
void* callData);

std::string key = "";

int main(int, char* [])
   {
      // Create a sphere
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<vtkCallbackCommand> keypressCallback =
	vtkSmartPointer<vtkCallbackCommand>::New();
keypressCallback->SetCallback(KeypressCallbackFunction);
renderWindowInteractor->AddObserver(vtkCommand::KeyPressEvent, keypressCallback);
keypressCallback->SetClientData(sphereSource);

//key = renderWindowInteractor->GetKeySym();
renderer->InteractiveOn();
renderer->AddActor(actor);
renderWindowInteractor->Initialize();
renderer->SetBackground(1, 1, 1); // Background color white
renderWindow->Render();
renderWindowInteractor->Start();

return EXIT_SUCCESS;
}

 void KeypressCallbackFunction(vtkObject* caller, long unsigned int 
 vtkNotUsed(eventId), void* clientData, void* callData)
{
std::cout << "Keypress callback" << std::endl;

vtkRenderWindowInteractor* iren =
	static_cast<vtkRenderWindowInteractor*>(caller);

std::cout << "Pressed: " << iren->GetKeySym() << std::endl;

key = iren->GetKeySym();

vtkSphereSource* sphere =
	static_cast<vtkSphereSource*>(clientData);

	if (key == "Right") {
		std::cout << "TEST RIGHT" << std::endl;

		sphere->SetRadius(10);
		sphere->Modified();
		sphere->Update();

		
		

	}

  }

```

The problem is that when I run this code, the action connected to the keypress is starting only after that I click the right arrow and after a click with the mouse. I would like to start the action only with the right arrow, not also with a second click.  
What is the problem? I didn’t find anything online for solving this, please help.

Thanks in advance
