Key input in Irrlicht

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
mtbal
Posts: 9
Joined: Wed Aug 26, 2009 8:41 pm

Key input in Irrlicht

Post by mtbal »

How do I get key input in Irrlicht? They do it in the Terrain tutorial, but that is when a key is tapped. I want it to check in the main loop to see if a key is pressed, and then do my code. (I'm trying to make it so that when I hold a key down, the node is rotated.) BTW this is in C#. But I can prolly translate any C++ code.

Thanks!

~mtbal
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Check example 4, IIRC that's the tutorial with the key array method in the receiver.
mtbal
Posts: 9
Joined: Wed Aug 26, 2009 8:41 pm

Post by mtbal »

hybrid wrote:Check example 4, IIRC that's the tutorial with the key array method in the receiver.
Sorry, but that still moves when the key is clicked. Any other ideas?
THX
pc0de
Posts: 300
Joined: Wed Dec 05, 2007 4:41 pm

Re: Key input in Irrlicht

Post by pc0de »

mtbal wrote:... I want it to check in the main loop to see if a key is pressed, and then do my code....
Irrlicht doesn't have a function for determining key state. Instead, you need to keep track of it yourself. Like hybrid mentioned, an example of how to do so can be found in 04.movement. See IsKeyDown():

Code: Select all

	while(device->run())
	{
		// Work out a frame delta time.
		const u32 now = device->getTimer()->getTime();
		const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
		then = now;

		/* Check if keys W, S, A or D are being held down, and move the
		sphere node around respectively. */
		core::vector3df nodePosition = node->getPosition();

		if(receiver.IsKeyDown(irr::KEY_KEY_W))
			nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime;
		else if(receiver.IsKeyDown(irr::KEY_KEY_S))
			nodePosition.Y -= MOVEMENT_SPEED * frameDeltaTime;


mtbal
Posts: 9
Joined: Wed Aug 26, 2009 8:41 pm

Re: Key input in Irrlicht

Post by mtbal »

Thanks!! :D
XXChester
Posts: 95
Joined: Thu Oct 04, 2007 5:41 pm
Location: Ontario, Canada

Post by XXChester »

Another way to handle this is to track the key's last state. In a demo I wrote I kept track of boolean's for the key press. If the next time it went into the event receiver and I still had the "a" key pressed; boolean stillPressed = true; And than after that I would do my world manipulations.

so....

//Global variable
boolean wPressed = false;

//function
if (receiver.IsKeyDown(irr::KEY_KEY_W)) {
wPressed = true;
}

if (wPressed) {
nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime;
}
Post Reply