GetKeySym() functionality not working when using to customise interaction styles

I am using VTK in C++ for the project with the interactor style vtkInteractorStyleTrackballCamera. Thereafter, I am trying to override the functionalities to create a custom interaction style. Herein is my code for custom interaction style.

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

  virtual void OnLeftButtonDown() override
  {
    std::cout << "Pressed left mouse button." << std::endl;

  }

  virtual void OnMiddleButtonDown() override
  {
    std::cout << "Pressed middle mouse button." << std::endl;
    // Forward events
    vtkInteractorStyleTrackballCamera::OnLeftButtonDown();

    vtkRenderWindowInteractor* rwi = this->Interactor;
    std::string key = rwi->GetKeySym();
    std::cout << "KEYY: " << key << "\n";
    if(key == "Ctrl") {
      std::cout << "panning started" << std::endl;
      vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
      vtkInteractorStyleTrackballCamera:: OnMiddleButtonDown();
      std::cout << "panning stopped" << std::endl;
    }

  }

  virtual void OnMiddleButtonUp() override
  {
    std::cout << "Left the middle mouse button." << std::endl;
    vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
    vtkInteractorStyleTrackballCamera:: OnMiddleButtonUp();

  }

  virtual void OnRightButtonDown() override
  {
    std::cout << "Pressed right mouse button." << std::endl;

  }
  
};

vtkStandardNewMacro(customMouseInteractorStyle);

The problem I seem to be facing pertains to the function onMiddleButtonDown(). Herein, when I try getting the key that’s pressed using vtkRenderWindowInteractor* rwi = this->Interactor; std::string key = rwi->GetKeySym(); , I always seem to be getting a blank string, this is what I receive on cout:

I was wondering if I am doing anything wrong when it comes to using the functionalities? And how do I get the expected behavior in such a scenario?

This is the example I followed to get the key that’s pressed: examples.vtk.org/site/Cxx/Interaction/KeypressEvents/.

Hello @adityaDas!

Does it print the key sym if you hold down an alphabet key while the middle button is pressed simultaneously?

vtkRenderWindowInteractor* rwi = this->Interactor;
std::string key = rwi->GetKeySym();
std::cout << "KEYY: " << key << “\n”;
if(key == “Ctrl”) {
std::cout << “panning started” << std::endl;
vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
vtkInteractorStyleTrackballCamera:: OnMiddleButtonDown();
std::cout << “panning stopped” << std::endl;
}

It looks like you’re interested in knowing whether Ctrl was pressed. In this case, can you try rwi->GetControlKey() instead of the key sym?

Hello @jaswantp! Thank you for getting back to me. What we noticed was that for both GetKeySym() and GetControlKey(), it is working in a native C++ environment (GetKeySym only records alphabets though). However, neither of them seem to be working in a wasm environment. Furthermore, in the wasm environment, even the default interaction styles do not seem to be working if it involves keypress. For an instance, when we use vtkInteractorStyleTrackballCamera, and we press the left mouse button down alongside pressing shift and/or ctrl, it is supposed to pan, spin, dolly or rotate based on the conditions as shown below.


However, it always enters the final else loop and just rotates, indicating that none of the keypress were effective in a wasm environment.

Looks like your html canvas doesn’t have focus to receive keyboard input. Can you share the canvas setup code? For reference, see line 81 and below for the correct way to setup your canvas https://github.com/Kitware/vtkWasmBenchmarks/blob/ecbd74c724c6be9c8b8c8c4f87d521f946edb042/Cones/web/app.js#L81

1 Like

Hello @jaswantp,

Thanks for your assistance and pointing out where the problem was. It was indeed how the canvas was set up in the project.