Proper way to consume mouse/keyboard events

For help with anything that CEGUI doesn't offer straight out-of-the-box, e.g.:
- Implementation of new features, such as new Core classes, widgets, WindowRenderers, etc. ...
- Modification of any existing features for specific purposes
- Integration of CEGUI in new engines or frameworks and writing of new plugins (Renderer, Parser, ...) or modules

Moderators: CEGUI MVP, CEGUI Team

User avatar
davesh
Not too shy to talk
Not too shy to talk
Posts: 36
Joined: Fri Feb 03, 2006 22:59

Proper way to consume mouse/keyboard events

Postby davesh » 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.

User avatar
saetrum
Quite a regular
Quite a regular
Posts: 55
Joined: Wed Jan 12, 2005 12:06
Location: Albuquerque, NM USA

Re: Proper way to consume mouse/keyboard events

Postby saetrum » Wed Feb 08, 2006 21:51

This is the way that I handle it

Code: Select all

if (!gGUISystem->injectMouseButtonDown(State::convertOgreButtonToCegui(e->getButtonID())))
   {
      // Left mouse button down
      if (e->getButtonID() & InputEvent::BUTTON0_MASK)
      {      
         mLMouseDown = true;
      } // if
      // Right mouse button down
      else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
      {
         mRMouseDown = true;
      } // else if
      else if (e->getButtonID() & InputEvent::BUTTON2_MASK)
      {
      }
   }
   e->consume();


Essentially all you have to do is check the return value of the inject<event> call. If it is true, then CEGUI handled it, if it is false no window handled the event.

HTH
There are 10 types of people in the world: those that understand binary and those that don't.


Return to “Modifications / Integrations / Customisations”

Who is online

Users browsing this forum: No registered users and 6 guests