A question about key input

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
Me

A question about key input

Post by Me »

Hello,

I have this code:

Code: Select all

class MyEventReceiver : public IEventReceiver{
public:
	virtual bool OnEvent(SEvent event) {
		if(animNode != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown) {
			switch(event.KeyInput.Key)
			{
			case KEY_KEY_W :
			case KEY_KEY_S : {
				vector3df v = animNode->getPosition();
				v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
				animNode->setPosition(v);
				}//end case
			}//end switch
		}//end if

    return false;
	}//end method
};//end class
And it works, but not the way that I want it to. When I press and release W or S it moves the object up or down respectively. I want it, however, to be constant feedback. That while I'm holding down the key, it continues to move the object. I've been trying to figure it out for a while, and can't. Any suggestions?

Thanx,
Me.
Saalen
Posts: 51
Joined: Thu Sep 04, 2003 7:49 am
Location: Germany
Contact:

Re: A question about key input

Post by Saalen »

You have to change a state variable when one of the keys is pressed.
During the loop, you check the state and modify the position at every iteration of the game loop.

Your code does not work because your conditions turn to false once you released the keys.
knightoflight
Posts: 199
Joined: Sun Aug 24, 2003 5:47 pm
Location: Germany

Post by knightoflight »

maybe you should delete this ? "&& !event.KeyInput.PressedDown"
Boogle
Posts: 162
Joined: Fri Nov 21, 2003 3:16 pm
Location: Toronto, Canada

Post by Boogle »

The following should work.. The 'event.KeyInput.PressedDown' event is spawned for every game loop iteration that the key is pressed down.. the '!event.KeyInput.PressedDown' is only spawned once, when you release the key.


class MyEventReceiver : public IEventReceiver{
public:
virtual bool OnEvent(SEvent event) {
if(animNode != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown) {
switch(event.KeyInput.Key)
{
case KEY_KEY_W :
case KEY_KEY_S : {
vector3df v = animNode->getPosition();
v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
animNode->setPosition(v);
}//end case
}//end switch
}//end if

return false;
}//end method
};//end class
saigumi
Posts: 921
Joined: Fri Aug 22, 2003 11:31 am
Location: St. Louis, MO USA
Contact:

Post by saigumi »

And for an explanation of why this is..

http://irrlicht.sourceforge.net/phpBB2/ ... ght=repeat
Crud, how do I do this again?
Post Reply