Header:
Code: Select all
#ifndef __GEVENTRECEIVER
#define __GEVENTRECEIVER
// Include headers
#include <irrlicht.h>
// Use namespaces
using namespace irr;
using namespace core;
// Event Receiver Class
class CEventReceiver : public IEventReceiver
{
public:
CEventReceiver();
virtual bool OnEvent(SEvent Event);
void resetKeys();
bool getKeyState(EKEY_CODE key);
bool getKeyStateOnUp(EKEY_CODE key);
private:
bool keysOnUp[KEY_KEY_CODES_COUNT];
bool keys[KEY_KEY_CODES_COUNT];
};
#endif
Code: Select all
// Include headers
#include "GEventreceiver.h"
// Constructor
CEventReceiver::CEventReceiver()
{
// Set the keypress array to false
for (s32 i = 0; i < KEY_KEY_CODES_COUNT; i++)
{
keysOnUp[i] = false;
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;
keysOnUp[Event.KeyInput.Key] = !Event.KeyInput.PressedDown;
}
if (Event.EventType == EET_MOUSE_INPUT_EVENT)
{
keys[Event.KeyInput.Key] = Event.KeyInput.PressedDown;
}
// Return false - ensures FPS cameras will still work
return false;
}
void CEventReceiver::resetKeys()
{
for (s32 i = 0; i < KEY_KEY_CODES_COUNT; i++)
{
keys[i] = false;
keysOnUp[i] = false;
}
}
// Get key state
bool CEventReceiver::getKeyState(EKEY_CODE key)
{
return keys[key];
}
bool CEventReceiver::getKeyStateOnUp(EKEY_CODE key)
{
bool x = keysOnUp[key];
keysOnUp[key] = false;
return x;
}