Slow down keyboard input

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
twilight17
Posts: 362
Joined: Sun Dec 16, 2007 9:25 pm

Slow down keyboard input

Post 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:
Post this userbar I made on other websites to show your support for Irrlicht!
Image
http://img147.imageshack.us/img147/1261 ... wernq4.png
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Just check the current time and ignore key presses which come too fast?
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post 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;
}
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Katsankat
Posts: 178
Joined: Sun Mar 12, 2006 4:15 am
Contact:

Post by Katsankat »

Code: Select all

if(receiver.IsKeyDown(KEY_KEY_F))
 light on;
else
 light off
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post 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.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
twilight17
Posts: 362
Joined: Sun Dec 16, 2007 9:25 pm

Post 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
Post this userbar I made on other websites to show your support for Irrlicht!
Image
http://img147.imageshack.us/img147/1261 ... wernq4.png
SwitchCase
Posts: 170
Joined: Sun Jul 01, 2007 11:41 pm
Location: Manchester, UK

Post by SwitchCase »

switch the light on and off on keyup rather than keydown but only if the previous input was keydown :D
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Good lord isn't this a really simple problem?

You just toggle the light on/off when the key is pressed....
Image Image Image
twilight17
Posts: 362
Joined: Sun Dec 16, 2007 9:25 pm

Post 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:
Post this userbar I made on other websites to show your support for Irrlicht!
Image
http://img147.imageshack.us/img147/1261 ... wernq4.png
twilight17
Posts: 362
Joined: Sun Dec 16, 2007 9:25 pm

Post 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;
Post this userbar I made on other websites to show your support for Irrlicht!
Image
http://img147.imageshack.us/img147/1261 ... wernq4.png
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

wtf
Thats exactly the same code i posted ealier.....
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
geckoman
Posts: 143
Joined: Thu Nov 27, 2008 11:05 am
Location: Germany
Contact:

Post 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...
twilight17
Posts: 362
Joined: Sun Dec 16, 2007 9:25 pm

Post by twilight17 »

Well it won't work without the else if. xD so Thanks Sudi, (mainly).

But whatever guys, it works :lol: :P
Post this userbar I made on other websites to show your support for Irrlicht!
Image
http://img147.imageshack.us/img147/1261 ... wernq4.png
geckoman
Posts: 143
Joined: Thu Nov 27, 2008 11:05 am
Location: Germany
Contact:

Post 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;
}


Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

Post 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;
Professional Software Developer and Amateur Game Designer ;-)
Post Reply