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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

ochampao
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.







--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: Callback method to handle LeftButtonPressEvent + 'keyboard char-key pressed'

David Gobbi
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 <[hidden email]> 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.

_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: Callback method to handle LeftButtonPressEvent + 'keyboard char-key pressed'

ochampao
Hi David,

Thanks a lot for your reply. I suspected that those methods were valid
within KeyPress events only. It works now.

P.




--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
https://public.kitware.com/mailman/listinfo/vtkusers