Page 1 of 1

Slow down keyboard input

Posted: Wed Dec 17, 2008 3:49 am
by twilight17
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

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];
};
and the code to turn the flashlight on/off

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;
			}
		
		}
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 :mrgreen:

Posted: Wed Dec 17, 2008 8:30 am
by hybrid
Just check the current time and ignore key presses which come too fast?

Posted: Wed Dec 17, 2008 9:12 am
by sudi
Na don't use time checks. Maybe this works.

Code: Select all

int LightSwitch = 0; //1 is ON and 0 is OFF

bool LastState = false;

 (This is in while (device->run().. part) 

bool State = receiver.IsKeyDown(KEY_KEY_F);

if (State != LastState && State)
{
     if (LightSwitch == 0)
     {
            FlashLight->setVisible(true);
            LightSwitch = 1;
     }
     else
     {
            FlashLight->setVisible(false);
            LightSwitch = 0;
     }
     LastState = State;
}

Posted: Wed Dec 17, 2008 9:34 am
by Katsankat

Code: Select all

if(receiver.IsKeyDown(KEY_KEY_F))
 light on;
else
 light off

Posted: Wed Dec 17, 2008 9:39 am
by sudi
Katsankat wrote:

Code: Select all

if(receiver.IsKeyDown(KEY_KEY_F))
 light on;
else
 light off
no that will turn the light off when u let go of the button.

Posted: Wed Dec 17, 2008 2:13 pm
by twilight17
Thanks Sudi, but using that code makes the light turn on the way I want, but it's impossible to turn it off. I'll experiment laater when I'm back from school.

Thanks everyone

Posted: Wed Dec 17, 2008 6:32 pm
by SwitchCase
switch the light on and off on keyup rather than keydown but only if the previous input was keydown :D

Posted: Wed Dec 17, 2008 7:35 pm
by JP
Good lord isn't this a really simple problem?

You just toggle the light on/off when the key is pressed....

Posted: Wed Dec 17, 2008 8:10 pm
by twilight17
JP wrote:Good lord isn't this a really simple problem?

You just toggle the light on/off when the key is pressed....
Yes i know, but the input is very quick. It turns the light on and off rapidly. I'm going to mess around now, as school is out. I'll post with results soon.

Edit: @SwitchCase: I'll see if I can manage that. :lol:

Posted: Wed Dec 17, 2008 9:11 pm
by twilight17
I got it! Thanks everyone

The code I used:

(The EventReceiver above)

&

[Before while (device->run()) ]

Code: Select all

	bool LightSwitch = false; //true is ON and false is OFF
	bool LastState = false; 
	bool State = receiver.IsKeyDown(KEY_KEY_F);

[in while (device->run()) ]

Code: Select all

		State = receiver.IsKeyDown(KEY_KEY_F); 


			if (LightSwitch == false && State != LastState && State)
			{
				FlashLight->setVisible(true);
				LightSwitch = true;
			}
			else if (LightSwitch == true && State != LastState && State)
			{
				FlashLight->setVisible(false);
				LightSwitch = false;
			}
			LastState = State;

Posted: Wed Dec 17, 2008 9:14 pm
by sudi
wtf
Thats exactly the same code i posted ealier.....

Posted: Wed Dec 17, 2008 10:46 pm
by geckoman
No it's more complicated. More USELESS conditions :)

Code: Select all

else if (LightSwitch == true
thats to much, if it is not false it is true...

Posted: Thu Dec 18, 2008 3:24 am
by twilight17
Well it won't work without the else if. xD so Thanks Sudi, (mainly).

But whatever guys, it works :lol: :P

Posted: Thu Dec 18, 2008 8:03 am
by geckoman
That should do the same as your if-statement (I was too tired to write yesterday)

Code: Select all


if (State != LastState && State)
{
	FlashLight->setVisible(LightSwitch);
	LightSwitch = !LightSwitch;
}



Posted: Wed Sep 23, 2009 1:59 pm
by Auradrummer
Another simple method for one is having this problem:

Code: Select all

        if (receiver.IsKeyDown (KEY_KEY_C))
        {
            if (selectOn == false)
            {
                   <what the key makes>
            };
            selectOn = true;
        }
        else
            selectOn = false;