[vtkusers] Catching arrow keys in linux

David Gobbi david.gobbi at gmail.com
Fri Jan 22 16:17:24 EST 2010


On Fri, Jan 22, 2010 at 2:06 PM, David Doria <daviddoria+vtk 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>
>> 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>
>> >> 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



More information about the vtkusers mailing list