// Include headers
#include <CEventReceiver.h>
// Constructor
CEventReceiver::CEventReceiver()
{
// Set the keypress array to false
for (s32 i = 0; i < KEY_KEY_CODES_COUNT; i++)
{
keys[i] = false;
}
}
// Event handler function
bool CEventReceiver::OnEvent(SEvent Event)
{
// If the event type is a key input event
if (Event.EventType == EET_KEY_INPUT_EVENT)
{
// Set the corresponding value in the array to the state of the key
keys[Event.KeyInput.Key] = Event.KeyInput.PressedDown;
}
// Return false - ensures FPS cameras will still work
return false;
}
// Get key state
bool CEventReceiver::getKeyState(EKEY_CODE key)
{
return keys[key];
}
That's pretty much it. In your main loop, you can check the status of keys like so:
very good thanks
but i got a little prob (think its not related to your code)
what did i do wrong if it says theres a double definition of the constructor and the 2 functions
Thanks Conquistador, that works nicely! For those of you who tend to forget things, like me, don't forget to register the event receiver in your createDevice call.
is there a way to register more than on reciever with the createdevice call?
or in other words, somehow to make it modular so you only have to add the events that are needed (instead of recreating one everytime or adding everything)
class CEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent Event)
{
if (Event.EventType == EET_KEY_INPUT_EVENT)
{
// Do something
}
else if (Event.EventType == EET_MOUSE_INPUT_EVENT)
{
// Do something else
}
}
};
There, the receiver is set up to only handle key and mouse events, and it will only execute the commands corresponding to the type of event it is. As far as recreating one for every program, you could make a basecode for an event receiver, handling only simple events using a method you'll use for every project, then just modify it and add the things that suit your needs. That's how I do it.
Last edited by Conquistador on Mon Jan 09, 2006 1:36 pm, edited 1 time in total.
if (Event.EventType == EET_KEY_INPUT_EVENT)
{
// Do something
}
else if (Event.EventType == EET_KEY_INPUT_EVENT)
{
// Do something else
}
Ok, I have a question, if EET_KEY_INPUT_EVENT in the first if evaluates out to false, then wouldn't the second if(EET_KEY_INPUT_EVENT) also be false thus you would never end up in the second if? and thus a bit redundant? Or am I just reading the code wrong?
// Constructor
CEventReceiver::CEventReceiver()
{
// Set the keypress array to false
for (s32 i = 0; i < KEY_KEY_CODES_COUNT; i++)
{
keys[i] = false;
}
}
but ive a problem also the array stays false if i press a button