Page 1 of 1
Multiple-key input?
Posted: Wed May 10, 2006 12:33 pm
by Hellfish
Hey. I hope this q hasn't been posted before, but I couldn't find it searching so..
Currently I have, through looking at the example, setup a scene with a terrain and a model for the player to control. I've gotten the player to collide with the terrain,the camera to follow the player and the player to move according to keyboard input.
The problem is that it only responds to one key at a time, unless two keys were pressed simultaneously (i.e. pressing A turns left, but pressing W while holding down A stops turning and only moves forward. But pressing both simultaneously would move the player forward at the same time as turning him.)
Does anyone have a suggestion on how to allow the player to turn while still moving forward without having to press the two keys down simultaneously?
Posted: Wed May 10, 2006 3:15 pm
by Cube3
You would typically hold the keys in a buffer aray of like say 256 elements, which contains boolean's (pressed or not pressed).
The element index, corresponds to the keycode. I am sure there's something posted on this.
Posted: Fri May 12, 2006 7:09 pm
by RapchikProgrammer
I think you are making an if condition like this:
if Left Key then
Move left
elseif Right key then
Move Right
elseif Up key then
Move Forward
elseif Down key then
Move backward
end if
if you have a condition like this make it like this:
if Left Key then
Move left
end if
if Right key then
Move Right
end if
if Up key then
Move Forward
end if
if Down key then
Move backward
end if
Posted: Tue May 16, 2006 8:08 am
by Hellfish
Thanks for replying!
The problem is that only one key is recieved through the parameter of the event-handler method. So with your method, RapchikProgrammer, only one clause is entered and the same behaviour happens.
Cube3, for the same reason, I'm unsure how that keyarray would help, seeing as I can't find out if a key is released, only pressed down and would have to reset the array on intervals. And if that interval was too short, the same behaviour would appear, but too long and the action would continue til after the key was released.
Posted: Wed May 17, 2006 2:52 pm
by Cube3
If you are willing to do your own implementation instead of irrlicht's, you could take a look at
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=12301
Posted: Thu May 18, 2006 8:22 am
by Hellfish
Thanks, that did the trick!
Now I'll just have to put some thought into understanding it.

Posted: Fri Nov 03, 2006 8:17 pm
by forrestcupp
Seems like it would be a lot easier and shorter to just use Managed DirectInput.
Edit:
I tried DirectInput and it isn't compatible with Irrlicht. What a disappointment!
Posted: Fri Nov 17, 2006 5:05 pm
by Cube3
Seems Like I didn't want a dependency to Managed Direct Input.
Edit:
Seem Like you didn't want it either after all.

Posted: Sun Dec 17, 2006 4:23 am
by BLiTZWiNG
I added this code to my project and it kept generating exceptions inside Irrlicht (and not processing my keys), which I have not been able trace it because even if I alt-tab, the app still has my mouse locked to the center of the screen. Admittedly, I haven't tried to work around that as yet, I've been busier trying to understand how the KeyboardListener class works.
Posted: Mon Dec 18, 2006 10:21 pm
by jun
Hellfish wrote:The problem is that it only responds to one key at a time, unless two keys were pressed simultaneously (i.e. pressing A turns left, but pressing W while holding down A stops turning and only moves forward.
I don't have that problem. Just have your event handler set boolean variables and then check them in your rendering loop. Make sure to use if(){} and not if(){}else if(){} . You are probably calling your actions from the event handler, which will make them be one action per keypress or one action per entrance to the even handler. Setting up which action to call in event handler then actually calling in render loop works, and it's easier than monkeying with directx directly.
Code: Select all
bool moveForward = false;
bool rotating = false;
float rotateBy = 0.0f;
...
while(device->run() && !ExitFlag)
if (device->isWindowActive())
{
driver->beginScene(true, true, 0 );
if(moveForward)
{
moveF(node);
}
if(rotating)
{
rotate(node, rotateBy);
}
smgr->drawAll();
env->drawAll();
driver->endScene();
}
Hellfish wrote:Cube3, for the same reason, I'm unsure how that keyarray would help, seeing as I can't find out if a key is released, only pressed down and would have to reset the array on intervals.
Keypress is event.KeyInput.PressedDown, so release is !event.KeyInput.PressedDown
Code: Select all
bool OnEvent(SEvent event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
{
if(event.KeyInput.PressedDown)
{
switch (event.KeyInput.Key)
{
case irr::KEY_SHIFT:
rotating = true;
break;
case irr::KEY_KEY_W:
moveForward = true;
break;
}
else /* key released */
{
switch (event.KeyInput.Key)
{
case irr::KEY_SHIFT:
rotating = false;
break;
case irr::KEY_KEY_W:
moveForward = false;
break;
}
}
}