Xaryl said, it is most likely because the keyboard only fires events when the key is pressed or released. It does not continuously fire events indicating that a specific key is held down.
Depending on how flexible you want your event management system to be, you could store only information regarding a select few keys, or you could do something like I have done.
I actually just finished writing this a few days ago for a project I'm working on, but you are more than free to use it if you want.
(Keep in mind that for my purposes, I only needed the left mouse button, so my class doesn't deal with other mouse buttons at all).
EventManager.h
Code: Select all
#ifndef EVENT_MANAGER_H_
#define EVENT_MANAGER_H_
#include <irrlicht.h>
class EventManager : public irr::IEventReceiver
{
public:
// Constructor
EventManager();
// The overriden virtual method
bool OnEvent(const irr::SEvent& m_event);
// Methods for checking mouse states and position
bool isMouseDown();
bool isMousePressed();
bool isMouseReleased();
int mouse_x();
int mouse_y();
// Methods for checking key states
bool isKeyDown(irr::EKEY_CODE keyCode);
bool isKeyPressed(irr::EKEY_CODE keyCode);
bool isKeyReleased(irr::EKEY_CODE keyCode);
// Reset the EventManager
// Do it at the end of the Game loop
void reset();
private:
bool keyDown[irr::KEY_KEY_CODES_COUNT];
bool keyPressed[irr::KEY_KEY_CODES_COUNT];
bool keyReleased[irr::KEY_KEY_CODES_COUNT];
bool mouseDown;
bool mousePressed;
bool mouseReleased;
int mouseX;
int mouseY;
};
#endif
EventManager.cpp
Code: Select all
#include "EventManager.h"
/*
* Create an EventManager
*/
EventManager::EventManager()
{
mouseDown = false;
mousePressed = false;
mouseReleased = false;
mouseX = 0;
mouseY = 0;
// Set all key states to "up"
for (int i = 0; i < irr::KEY_KEY_CODES_COUNT; i++)
{
keyDown[i] = false;
keyPressed[i] = false;
keyReleased[i] = false;
}
}
/*
* Handle incoming Input Events
*/
bool EventManager::OnEvent(const irr::SEvent& m_event)
{
// Check if the Input Event is Key related
if (m_event.EventType == irr::EET_KEY_INPUT_EVENT)
{
// Get the Key that triggered the Event
irr::EKEY_CODE key = m_event.KeyInput.Key;
bool down = m_event.KeyInput.PressedDown;
// Check if the Key is down
if (down)
{
// Check if the key was just pressed
if (!keyDown[key])
{
keyPressed[key] = true;
}
keyDown[key] = true;
}
else
{
// Check if the key was just released
if (keyDown[key])
{
keyReleased[key] = true;
}
keyDown[key] = false;
}
}
// Check if the Input Event is Mouse related
else if (m_event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
// Check if the left mouse button is pressed
bool pressed = m_event.MouseInput.isLeftPressed();
// Set member variables accordingly
if (pressed)
{
// Check if the button was just pressed
if (!mouseDown)
{
mousePressed = true;
}
mouseDown = true;
}
else
{
// Check if the button was just released
if (mouseDown)
{
mouseReleased = true;
}
mouseDown = false;
}
// Update the Mouse position
mouseX = m_event.MouseInput.X;
mouseY = m_event.MouseInput.Y;
}
return false;
}
/*
* Check if the Left MB is down
*/
bool EventManager::isMouseDown()
{
return mouseDown;
}
/*
* Check if the Left MB was just pressed
*/
bool EventManager::isMousePressed()
{
return mousePressed;
}
/*
* Check if the Left MB was just released
*/
bool EventManager::isMouseReleased()
{
return mouseReleased;
}
/*
* Get the X location of the Mouse
*/
int EventManager::mouse_x()
{
return mouseX;
}
/*
* Get the Y location of the Mouse
*/
int EventManager::mouse_y()
{
return mouseY;
}
/*
* Check if a Key is Down
*/
bool EventManager::isKeyDown(irr::EKEY_CODE keyCode)
{
return keyDown[keyCode];
}
/*
* Check if a Key was just pressed
*/
bool EventManager::isKeyPressed(irr::EKEY_CODE keyCode)
{
return keyPressed[keyCode];
}
/*
* Check if a Key was just released
*/
bool EventManager::isKeyReleased(irr::EKEY_CODE keyCode)
{
return keyReleased[keyCode];
}
/*
* Reset the EventManager (clear mousePressed and mouseReleased)
*/
void EventManager::reset()
{
mousePressed = false;
mouseReleased = false;
// Reset the KeyPressed and KeyReleased states
for (int i = 0; i < irr::KEY_KEY_CODES_COUNT; i++)
{
keyPressed[i] = false;
keyReleased[i] = false;
}
}
To use it, simply create an instance of EventManager and pass it to Irrlicht. Then use any of the functions, for example "isKeyDown()" to check if a key is being held down. To clarify "isKeyPressed" checks for the one time event that is fired when a user first presses a key, "isKeyReleased" checks for the one time event that is fired when a user releases a key, and "isKeyDown" checks if a user has pressed a key, but not released it.
You will also need to call another function "reset()". Call this function at the end of your Game Loop. For example, your loop might look like:
Code: Select all
while (GameIsRunning)
{
gameLogic();
render();
myEventManager.reset();
}
Hope that helps (and if anyone else has any problems with my code, or has a better solution, feel free to let me know)!