[vtkusers] Callback method to handle LeftButtonPressEvent + 'keyboard char-key pressed'

David Gobbi david.gobbi at gmail.com
Mon Jun 18 16:06:53 EDT 2018


Hi Panos,

The GetKeySym() and GetKeyCode() methods are only valid within Key event
callbacks.

If you need to know what key is held down, then you must also bind your
callback to KeyPressEvent and KeyReleaseEvent.  Then whenever there is a
KeyPress, you can store the key that was pressed in a variable, and in
KeyReleaseEvent, you can clear it.

Modifier keys (Ctrl, Shift, etc) are stored in a bitfield where each bit
represents a different key, but it's only the modifier keys that are
handled this way.

 - David




On Mon, Jun 18, 2018 at 9:22 AM, ochampao <ochampao at hotmail.com> wrote:

> Hi vtkUsers,
>
> In my code I have a callback function invoked when the
> vtkCommand::LeftButtonPressEvent event is emitted. Inside the callback, I
> would like to check if also a specific keyboard button is pressed. When I
> try to access the button being pressed via
> vtkRenderWindowInteractor::GetKeySym(), I get the following error:
>
> "Debug Assertion Failed! Program: ....MSVCP140D.dll, File: ...\xstring:
> Line
> 1181, Expression: invalid pointer".
>
> How can I correctly implement this behaviour?
>
>
> Here is a minimal callback method that causes the error:
>
> // ========================
> void MyLeftMousePressedCallback(
>         vtkObject* caller,
>         long unsigned int vtkNotUsed(eventId),
>         void* vtkNotUsed(clientData),
>         void* vtkNotUsed(callData))
> {
>         vtkRenderWindowInteractor *iren =
>                 static_cast<vtkRenderWindowInteractor*>(caller);
>
>         std::string key = iren->GetKeySym();
>
>         if ((key == "C") || (key == "c"))
>                 std::cout << "Do something if LeftMouseButton + C-key is
> pressed";
> }
> // ========================
>
> and this is how the callback is registered:
>
> // =========================
> vtkNew<vtkCallbackCommand> cbk;
> cbk->SetCallback(MyLeftMousePressedCallback);
> renderWindowInteractor->AddObserver(
>         vtkCommand::LeftButtonPressEvent, cbk);
> // =========================
>
> I have also tried extending vtkInteractorStyleImage and re-implementing the
> virtual method OnLeftButtonDown(), but I get the same error when using
> this->Interactor->GetKeySym(). In both cases, if GetKeySym() is replaced
> with GetKeyCode(), the returned value is always '\0'. Interestingly, using
> GetControlKey() works.
>
> Thanks for your help,
> Panos.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20180618/af5b2897/attachment.html>


More information about the vtkusers mailing list