Page 1 of 1

Key input problems

Posted: Fri Jul 27, 2007 2:40 am
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.

Posted: Fri Jul 27, 2007 5:23 am
by randomMesh
Try the forum search.

"bool AND keys" should give good results.

Posted: Fri Jul 27, 2007 9:37 pm
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));
}

Posted: Fri Jul 27, 2007 10:00 pm
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;
  }
.
.
.

Posted: Fri Jul 27, 2007 11:12 pm
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: