[vtkusers] Catching arrow keys in linux

David Doria daviddoria+vtk at gmail.com
Fri Jan 22 17:11:49 EST 2010


On Fri, Jan 22, 2010 at 4:17 PM, David Gobbi <david.gobbi at gmail.com> wrote:

> On Fri, Jan 22, 2010 at 2:06 PM, David Doria <daviddoria+vtk at gmail.com<daviddoria%2Bvtk at gmail.com>>
> wrote:
> > On Fri, Jan 22, 2010 at 3:47 PM, David Gobbi <david.gobbi at gmail.com>
> wrote:
> >>
> >> It's failing because you are doing this:
> >>
> >>  rwi->GetKeySym() == "Up"
> >>
> >> Throw in a strcmp or something.
> >>
> >>  - David
> >>
> >>
> >> On Fri, Jan 22, 2010 at 1:36 PM, David Doria <daviddoria+vtk at gmail.com<daviddoria%2Bvtk at gmail.com>
> >
> >> wrote:
> >> > On Fri, Jan 22, 2010 at 3:21 PM, David Gobbi <david.gobbi at gmail.com>
> >> > wrote:
> >> >>
> >> >> I'm not sure what #defines you are talking about, but why not use the
> >> >> interactor "GetKeySym" method to detect arrow keys?  They have the
> >> >> names "Up", "Down", "Left", "Right".
> >> >>
> >> >>    David
> >> >>
> >> >>
> >> >> On Fri, Jan 22, 2010 at 1:18 PM, David Doria <
> daviddoria+vtk at gmail.com <daviddoria%2Bvtk at gmail.com>>
> >> >> wrote:
> >> >> > It seems that for win32, there are some #defines for arrow keys
> like
> >> >> > VK_RIGHT. I can't seem to find what should be done for non win32 -
> >> >> > any
> >> >> > thoughts?
> >> >> >
> >> >> > Thanks,
> >> >> >
> >> >> > David
> >> >>
> >> >
> >> > So typically I do this from inside my InteractorStyle subclass:
> >> >
> >> >     virtual void OnChar()
> >> >     {
> >> >       vtkRenderWindowInteractor *rwi = this->Interactor;
> >> >
> >> >       char ch = rwi->GetKeyCode() ;
> >> >       switch (ch)
> >> >       {
> >> >         case 's':
> >> >           cout << "Pressed s." << endl;
> >> >           break;
> >> >         case 'a':
> >> >           cout << "Pressed a." << endl;
> >> >           break ;
> >> >         default:
> >> >           cout << "Pressed an unhandled key: " << rwi->GetKeyCode() <<
> >> > endl;
> >> >           break;
> >> >       }
> >> > The problem is that OnChar() doesn't fire when the arrow keys are
> >> > pressed. I
> >> > tried to use
> >> >     virtual void OnKeyDown()
> >> >     {
> >> >       vtkRenderWindowInteractor *rwi = this->Interactor;
> >> >
> >> >       if(rwi->GetKeySym() == "Up")
> >> >       {
> >> >         cout << "Pressed up." << endl;
> >> >       }
> >> >
> >> >       // forward events
> >> >       vtkInteractorStyleTrackballCamera::OnKeyDown();
> >> >     }
> >> > But it doesn't seem to compare to true. It also doesn't seem to flush
> >> > the
> >> > buffer when I would expect?
> >> > Also, what is the difference between KeyPress and KeyDown? The
> >> > documentation
> >> > is kind of broken
> >> >
> >> > here:
> http://www.vtk.org/doc/nightly/html/classvtkInteractorStyle.html#a65fcd9765c162a6021434386037ca641
> >> > Thanks,
> >> >
> >> > David
> >
> > Yes, that was certainly a bug, but not as confusing as the following
> > problem.
> > With these two functions:
> >
> >     virtual void OnKeyPress()
> >     {
> >       vtkRenderWindowInteractor *rwi = this->Interactor;
> >
> >       std::string key = rwi->GetKeySym();
> >       if(key.compare("Up") == 0)
> >         {
> >         cout << "Pressed up." << endl;
> >         }
> >
> >       // forward events
> >       vtkInteractorStyleTrackballCamera::OnKeyPress();
> >     }
> >
> >     virtual void OnChar()
> >     {
> >       vtkRenderWindowInteractor *rwi = this->Interactor;
> >
> >       char ch = rwi->GetKeyCode() ;
> >       switch (ch)
> >       {
> >         case 's':
> >           cout << "Pressed s." << endl;
> >           break;
> >         case 'a':
> >           cout << "Pressed a." << endl;
> >           break ;
> >         default:
> >           cout << "Pressed an unhandled key: " << ch << endl;
> >           break;
> >       }
> >  // forward events
> >       vtkInteractorStyleTrackballCamera::OnChar();
> >     }
> > The output of pressing:
> > a, b, up, a
> > Is:
> > Pressed a.
> > Pressed an unhandled key: b
> > Pressed up.
> > Pressed an unhandled key: Pressed a.
> > You can see that there is an "unhandled key" in the mix when there was
> > actually no unhandled key. There were only 4 keypresses, and 5 outputs.
> > Thanks,
> >
> > David
>
> Print out the keycode of the mystery key as a hexidecimal value.  My
> guess is that you're seeing an output from the arrow key in both the
> OnKeyPress and in OnChar.  The KeyCode values aren't guaranteed to be
> ascii.
>
>   David
>

Yea, I just though of that in the car :)

Using a single function does just fine:

    virtual void OnKeyPress()
    {
      //get the keypress
      vtkRenderWindowInteractor *rwi = this->Interactor;
      std::string key = rwi->GetKeySym();

      //output the key that was pressed
      cout << "Pressed " << key << endl;

      //handle an arrow key
      if(key.compare("Up") == 0)
        {
        cout << "The up arrow was pressed." << endl;
        }

      //handle a "normal" key
      if(key.compare("a") == 0)
        {
        cout << "The a key was pressed." << endl;
        }

      // forward events
      vtkInteractorStyleTrackballCamera::OnKeyPress();
    }

So two questions remain - 1) What is the difference between OnKeyPress and
OnKeyDown? 2) Why/when to use OnChar when OnKeyPress seems to be more
general?

Thanks for the help,

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100122/6ff4740b/attachment.htm>


More information about the vtkusers mailing list