Proper way to consume mouse/keyboard events
Posted: Tue Feb 07, 2006 13:06
I would like to understand the "proper" way to handle mouse events in an Ogre application that uses CEGUI. My question(s) follow the code segments that I embed below.
The following is a small code segment that illustrates a simplistic (but not adequate) mechanism for an Ogre Mouse event to be passed to CEGUI:
[color=330099]
void mousePressed (MouseEvent *e)
{
CEGUI::System::getSingleton().injectMouseButtonDown(
convertOgreButtonToCegui(e->getButtonID()));
e->consume();
}
void mouseReleased (MouseEvent *e)
{
CEGUI::System::getSingleton().injectMouseButtonUp(
convertOgreButtonToCegui(e->getButtonID()));
e->consume();
}
[/color]
The following is a similar section of code that illustrates capturing an Ogre keyboard event and passing it into CEGUI.
[color=330099]
void keyPressed(KeyEvent* e)
{
if(e->getKey() == KC_ESCAPE)
{
mShutdownRequested = true;
e->consume();
return;
}
CEGUI::System::getSingleton().injectKeyDown(e->getKey());
CEGUI::System::getSingleton().injectChar(e->getKeyChar());
e->consume();
}
void keyReleased(KeyEvent* e)
{
CEGUI::System::getSingleton().injectKeyUp(e->getKey());
e->consume();
}
[/color]
Neither of these code segments are adequate since indeed there are times when the mouse event needs to be passed into CEGUI and there are times that it needs to be handled at the application level to effect some action within the broader application (e.g. rotate a scene). Clearly when the user "clicks on a button" then this event needs to be passed to CEGUI. When a user clicks elsewhere (not on a CEGUI element/window) then this needs to be recognized and handled within the application. How does one recognize that CEGUI did not handle the mouse or keyboard event? Is it based on a return code from the call to one of the CEGUI functions; e.g. [color=FF0099] int rtn = CEGUI::System::getSingleton().injectMouseButtonDown() ; if (rtn == CEGUI_NOT_HANDLED) { Execute Application Logic Here } [/color]?
If so this would work well, this would only mean that I always need to pass the keyboard/mouse event into the corresponding CEGUI function ... check the return ... and then handle it at the application level if CEGUI did not handle it.
Please verify the correct method (and perhaps include a code snippet for reference).
Thanks in advance.
The following is a small code segment that illustrates a simplistic (but not adequate) mechanism for an Ogre Mouse event to be passed to CEGUI:
[color=330099]
void mousePressed (MouseEvent *e)
{
CEGUI::System::getSingleton().injectMouseButtonDown(
convertOgreButtonToCegui(e->getButtonID()));
e->consume();
}
void mouseReleased (MouseEvent *e)
{
CEGUI::System::getSingleton().injectMouseButtonUp(
convertOgreButtonToCegui(e->getButtonID()));
e->consume();
}
[/color]
The following is a similar section of code that illustrates capturing an Ogre keyboard event and passing it into CEGUI.
[color=330099]
void keyPressed(KeyEvent* e)
{
if(e->getKey() == KC_ESCAPE)
{
mShutdownRequested = true;
e->consume();
return;
}
CEGUI::System::getSingleton().injectKeyDown(e->getKey());
CEGUI::System::getSingleton().injectChar(e->getKeyChar());
e->consume();
}
void keyReleased(KeyEvent* e)
{
CEGUI::System::getSingleton().injectKeyUp(e->getKey());
e->consume();
}
[/color]
Neither of these code segments are adequate since indeed there are times when the mouse event needs to be passed into CEGUI and there are times that it needs to be handled at the application level to effect some action within the broader application (e.g. rotate a scene). Clearly when the user "clicks on a button" then this event needs to be passed to CEGUI. When a user clicks elsewhere (not on a CEGUI element/window) then this needs to be recognized and handled within the application. How does one recognize that CEGUI did not handle the mouse or keyboard event? Is it based on a return code from the call to one of the CEGUI functions; e.g. [color=FF0099] int rtn = CEGUI::System::getSingleton().injectMouseButtonDown() ; if (rtn == CEGUI_NOT_HANDLED) { Execute Application Logic Here } [/color]?
If so this would work well, this would only mean that I always need to pass the keyboard/mouse event into the corresponding CEGUI function ... check the return ... and then handle it at the application level if CEGUI did not handle it.
Please verify the correct method (and perhaps include a code snippet for reference).
Thanks in advance.