What is the correct way to handle keyboard 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
sd
Posts: 11
Joined: Wed Feb 27, 2008 4:18 am

What is the correct way to handle keyboard input?

Post by sd »

Hi there,

I have looked at the tutorial on movement (No#4) and also this:
http://www.irrlicht3d.org/wiki/index.ph ... ntReceiver
on bool keys type stuff.

But currently there are a few things I don't understand:

1. Often with the other postings on using bool keys, it seems the state of the key (up/down/pressed/released) is being checked in the IEventReceiver OnEvent method or whatever, but then where are the retreived states stored in the bool array actually used? Are they used in the:

while(device->run()) { ... }

loop?

Or reused in the OnEvent handler? But shouldn't you use some kind of game timing rather than just an keypress detector method?

2. How does ensure the 3D object being moved does so in a consistant timed way? Use an ITimer object or something?

3. Currently I have it all just done from the OnEvent method, changing the position of my 3D object, etc. When I first press the "Left" key and hold it, first the 3D object moves to the left a unit, then pauses a bit, then realizes the key is still held down and continues to move. Why does it have to pause? How to get around that?

Also as a side note, I was a bit confused trying to read the API documentation, and couldn't find anything on the beginScene thing or the SColor thing? Where do I find info on those to learn more?

Thanks,
SD.
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Re: What is the correct way to handle keyboard input?

Post by bitplane »

sd wrote: 1. Often with the other postings on using bool keys, it seems the state of the key (up/down/pressed/released) is being checked in the IEventReceiver OnEvent method or whatever, but then where are the retreived states stored in the bool array actually used? Are they used in the:

while(device->run()) { ... }
loop?
Yes, usually you'd check MyEventReceiver->AllKeys[KEY_KEY_WHATEVER] in your game loop.
sd wrote:2. How does ensure the 3D object being moved does so in a consistant timed way? Use an ITimer object or something?
Yes, store the time at the last loop (device->getTimer()->getTime()), and use the difference to work out how far you should move the actor (distance=direction*time)
sd wrote:3. Currently I have it all just done from the OnEvent method, changing the position of my 3D object, etc. When I first press the "Left" key and hold it, first the 3D object moves to the left a unit, then pauses a bit, then realizes the key is still held down and continues to move. Why does it have to pause? How to get around that?
There's no way to get around this when the movement code is inside your event receiver. Your event receiver code is called when a key is pressed or released, you need to move your actors every frame, if a key is pressed.
sd wrote: Also as a side note, I was a bit confused trying to read the API documentation, and couldn't find anything on the beginScene thing or the SColor thing? Where do I find info on those to learn more?
beginScene is called at the start of a render loop before drawing anything, SColor is just how the engine holds colours. Which part is confusing you?
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
sd
Posts: 11
Joined: Wed Feb 27, 2008 4:18 am

Excellent help, thankyou =)

Post by sd »

Thanks, that answers my questions and seems to be just what I was looking for =). Seems I didn't look hard enough into the API docs to find those functions.

Thanks,
SD.
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Re: What is the correct way to handle keyboard input?

Post by dlangdev »

sd wrote: 3. Currently I have it all just done from the OnEvent method, changing the position of my 3D object, etc. When I first press the "Left" key and hold it, first the 3D object moves to the left a unit, then pauses a bit, then realizes the key is still held down and continues to move. Why does it have to pause? How to get around that?

Thanks,
SD.
Try understanding the code posted below...

Code: Select all

bool CCameraActorFPSSceneNode::OnEvent(const SEvent& event)
{
	if (event.EventType == EET_KEY_INPUT_EVENT)
	{
		const u32 cnt = KeyMap.size();
		for (u32 i=0; i<cnt; ++i)
		{
			if (KeyMap[i].keycode == event.KeyInput.Key)
			{
				CursorKeys[KeyMap[i].action] = event.KeyInput.PressedDown; 
				
				actionState = KeyMap[i].action;

				if ( actionState != oldActionState )
				{
					if ( event.KeyInput.PressedDown )
					{
						if ( actionState == 0 )
						{
							nodeActor->setFrameLoop(1,14);
						}
						if ( actionState == 2 || actionState == 3 )
						{
							nodeActor->setFrameLoop(15,30);
						}
					}
					else
					{
						//nodeActor->setFrameLoop(184,205);
						nodeActor->setFrameLoop(206,250);
					}
					oldActionState = actionState;
				}
				else
				{
					if ( event.KeyInput.PressedDown == false )
					{
						//nodeActor->setFrameLoop(184,205);
						nodeActor->setFrameLoop(206,250);
						oldActionState = -1;
					}
					
				}

				if ( InputReceiverEnabled )
					return true;
			}
		}

		actionState = -1;
		if ( actionState != oldActionState )
		{
			if ( actionState = -1 )
			{
				//nodeActor->setFrameLoop(184,205);
				nodeActor->setFrameLoop(206,250);
			}
			oldActionState = actionState;
		}
	}

	return false;
}
Image
Image
Post Reply