Home › Forum › SOFA › Programming with SOFA › [SOLVED] Keyboard events in SOFA
- This topic has 5 replies, 4 voices, and was last updated 9 years, 5 months ago by
Hugo.
-
AuthorPosts
-
5 May 2016 at 16:40 #6675
jjcasmar
BlockedHello,
I’m trying to use the event handler to change some parameters of my componentes when some key is pressed, but the handleEvent method is not receiving any keypressed event.
Here is my code
template<class DataTypes> void LinearISOElasticityFEMAD<DataTypes>::handleEvent(sofa::core::objectmodel::Event *event) { // Key pressed event sofa::core::objectmodel::KeypressedEvent *ev = dynamic_cast<sofa::core::objectmodel::KeypressedEvent*>(event); std::cout << event->getClassName() << std::endl; if(ev != NULL) { std::cout << "Keyboard event" << std::endl; switch(ev->getKey()) { // The key E is pressed case 'e': //Something should happened here default: break; } } }The problem is that I’m never entering in the if statement. The print output says that I’m receiving this events
AnimateBeginEvent CollisionBeginEvent CollisionEndEvent IntegrateBeginEvent Optimizer solver IntegrateEndEvent AnimateEndEvent UpdateMappingEndEvent MouseEventntAny idea to get keyboard events?
8 May 2016 at 19:04 #6679Rosalie
BlockedHi jjcasmar,
Did you press the control key when you were trying to catch the letters?
Rosalie
8 May 2016 at 22:28 #6680jjcasmar
Blockeduhhh, no… should I?
9 May 2016 at 07:48 #6681Carrez
BlockedHi jjcasmar,
In fact the keyboard events are emitted by the viewer, when Ctrl is pressed (or Cmd on macs).
So, the viewer must have the focus (just click on the viewport once to be sure) ans you must press Ctrl+(or Cmd+,key> on a mac) and it should work. Bruno
9 May 2016 at 08:58 #6682Hugo
KeymasterHi all,
Just to add a small detail, you need to set the variable “f_listening” to true. This variable is common to all BaseObject. If true, this variable will allow the call of the handleEvent function in your component.
- either in your code: set “f_listening->setValue(true);” (hard-coded, then your component is always listening)
- or in the scene where your component is loaded : “< mycomponent listening="1" / >”
Hope this helps.
Hugo
6 June 2016 at 14:32 #7025Hugo
KeymasterHi JuanJo,
I think we answered your question about this point.
If you have any further questions or issues do not hesitate to create a new thread.Best,
Hugo
-
AuthorPosts
- You must be logged in to reply to this topic.
