Slow down keyboard input
Posted: Wed Dec 17, 2008 3:49 am
I'm setting up a simple flashlight for my project, and I have it set to turn on the flashlight whenever F is pressed (HL2 Style). The problem is that input is handled way too fast and that the light flickers extremely fast.
My code:
Event Handler
and the code to turn the flashlight on/off
My desired goal is to be able to hold the key down and it turns on, once I release and press it again, it will turn off.
Thank you
My code:
Event Handler
Code: Select all
class MyEventReceiver : public IEventReceiver
{
public:
// This is the one method that we have to implement
virtual bool OnEvent(const SEvent& event)
{
// Remember whether each key is down or up
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
return false;
}
// This is used to check whether a key is being held down
virtual bool IsKeyDown(EKEY_CODE keyCode) const
{
return KeyIsDown[keyCode];
}
MyEventReceiver()
{
for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
KeyIsDown[i] = false;
}
private:
// We use this array to store the current state of each key
bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
Code: Select all
int LightSwitch = 0; //1 is ON and 0 is OFF
(This is in while (device->run().. part)
if(receiver.IsKeyDown(KEY_KEY_F))
{
if (LightSwitch == 0)
{
FlashLight->setVisible(true);
LightSwitch = 1;
}
else
{
FlashLight->setVisible(false);
LightSwitch = 0;
}
}
Thank you