Key input problems

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
NoodlePowa
Posts: 32
Joined: Sun Jul 15, 2007 1:02 pm
Location: California

Key input problems

Post by NoodlePowa »

I'm using the code below for my simple 3D pong game:

Code: Select all

bool CEventReceiver::OnEvent(SEvent Event)
{
	if (Event.EventType == EET_KEY_INPUT_EVENT)
	{
		if (Event.KeyInput.Key == KEY_UP)
			p2Paddle->setPosition(p2Paddle->getPosition() + vector3df(0.0f, 0.0f, 1.0f));
		if (Event.KeyInput.Key == KEY_DOWN)
			p2Paddle->setPosition(p2Paddle->getPosition() + vector3df(0.0f, 0.0f, -1.0f));
		if (Event.KeyInput.Key == KEY_KEY_W)
			p1Paddle->setPosition(p1Paddle->getPosition() + vector3df(0.0f, 0.0f, 1.0f));
		if (Event.KeyInput.Key == KEY_KEY_S)
			p1Paddle->setPosition(p1Paddle->getPosition() + vector3df(0.0f, 0.0f, -1.0f));


		switch (Event.KeyInput.Key)
		{
		// Did the user press the "Esc" button?
		case 27:
			quit = true;
			break;
		}
	}
	return true;
}
However, I am having the following problems:

1. When you press and hold a key, let's say UP, the player 2 paddle goes up a tiny bit, and pauses. Then, it continues going up. It's the same situation if I was typing in a text editor like Word or NotePad. When you press a key, there is a pause before that key is repeated.

2. Even though I tried using if statements instead of a switch statement for the movement keys, only one would be processed.

Please help me.
"Be not ashamed of mistakes and thus make them crimes."
-Confucius
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

Try the forum search.

"bool AND keys" should give good results.
ucoskun
Posts: 8
Joined: Thu May 17, 2007 4:26 pm
Location: Turkey
Contact:

Post by ucoskun »

Code: Select all

bool wpressed;
bool spressed;

class CEventReceiver : public IEventReceiver
{
public:
	CEventReceiver()
	{
	}
	~CEventReceiver()
	{
	}
	bool OnEvent(SEvent event)
	{
		if(event.EventType == EET_KEY_INPUT_EVENT&&event.KeyInput.PressedDown == true)
		{

			if(event.KeyInput.Key == KEY_KEY_W)
			{
				
					wpressed=true;

				}
			
			if(event.KeyInput.Key == KEY_KEY_S)
			{			

                             spressed=true;
	}

		
		}
				else if(event.KeyInput.PressedDown == false)
				{
				wpressed=false;
				spressed=false;

				}



		return false;
	}


};
and

Code: Select all

while (device->run())
{
		
if (wpressed==true)
{
p2Paddle->setPosition(p2Paddle->getPosition() + vector3df(0.0f, 0.0f, 1.0f));
}
else if (spressed==true)
{
p2Paddle->setPosition(p2Paddle->getPosition() + vector3df(0.0f, 0.0f, -1.0f));
}
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

nice code ;)
but when a key is pressed you should set the other key to false:

Code: Select all

.
.
.
  if(event.KeyInput.Key == KEY_KEY_W)
  {
    wpressed=true;
    spressed=false;
  }
         
  if(event.KeyInput.Key == KEY_KEY_S)
  {         
    wpressed=false;
    spressed=true;
  }
.
.
.
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
NoodlePowa
Posts: 32
Joined: Sun Jul 15, 2007 1:02 pm
Location: California

Post by NoodlePowa »

Thanks for the replies. ^^

I searched through the forum and found a nice class.

Thanks again. I'll be sure to come back and bombard you with questions. :wink:
"Be not ashamed of mistakes and thus make them crimes."
-Confucius
Post Reply