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
Key input in Irrlicht
Re: Key input in Irrlicht
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():mtbal wrote:... I want it to check in the main loop to see if a key is pressed, and then do my code....
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;
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;
}
so....
//Global variable
boolean wPressed = false;
//function
if (receiver.IsKeyDown(irr::KEY_KEY_W)) {
wPressed = true;
}
if (wPressed) {
nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime;
}