Stop constant input from a held down key?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Culando
Posts: 12
Joined: Mon Jan 29, 2007 3:20 am

Stop constant input from a held down key?

Post by Culando »

Asking for help again as I can never seem to find this information on my own no matter how thoroughly I look.

Ok, I have this simple routine to shoot bullets, activated by the 'w' key.

Code: Select all

if ( receiver.Keys[ KEY_KEY_W ] ){
			IAnimatedMesh* mesh = smgr->getMesh("../../media/bullet1.x");
			IAnimatedMeshSceneNode* zap = smgr->addAnimatedMeshSceneNode( mesh );

//set various settings
			
	scene::ISceneNodeAnimator* anim2 = 0;

	// set flight line

	anim2 = smgr->createFlyStraightAnimator(core::vector3df(boxy_x,boxy_y,boxy_z),core::vector3df(-40,boxy_y,boxy_z), 3000,false);
	zap->addAnimator(anim2);	
	anim2->drop();
		}
It works like a charm, but will send out a constant stream of bullets if the 'w' key is held down.

How would I fix this routine so it will only fire once every time the key is pressed down?

I'm almost certain it's an obvious thing, but I can't find it anywhere.
Phant0m51
Posts: 106
Joined: Mon Jan 15, 2007 6:07 am

Post by Phant0m51 »

if (receiver.PressedDown == true && receiver.Key[KEY_KEY_W])

The reason your game acts as if the W is being pushed a ton of times is because of how operating systems auto-repeat characters when you hold them down. Using the 'receiver.PressedDown' stops it from repeating.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Phant0m51,

Most likely his Keys array is a bunch of bools indicating the current key state, so he is basically checking PressedDown already.

Culando,

If you only want one bullet per key press, then you need to check for the key to go from a not pressed state to the pressed state. Something like this...

Code: Select all

  bool bKeyWasPressed = false;

  while (device->run())
  {
    if (device->isWindowActive())
    {
      if ( receiver.Keys[KEY_KEY_W] && !bKeyWasPressed)
      {
        // create a bullet
      }

      bKeyWasPressed = receiver.Keys[KEY_KEY_W];

      if (driver->beginScene(...))
      {
        smgr->drawAll();
        driver->endScene();
      }
    }
  }

  device->drop();
  return 0;
}
Travis
Culando
Posts: 12
Joined: Mon Jan 29, 2007 3:20 am

Post by Culando »

Awesome, thanks a lot. I KNEW it'd be something simple. Only a few more things to fix and my bullet code will be finished and I can move on.
Post Reply