Multiple-key input?

Irrlicht.Net is no longer developed or supported, Irrlicht.Net Cross Platform is a much more complete wrapper. Please ask your C# related questions on their forums first.
Locked
Hellfish
Posts: 3
Joined: Wed May 10, 2006 12:25 pm
Location: Sweden

Multiple-key input?

Post 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?
Lordi in Eurovision Song Contest 06? Now we're getting somewhere!
Cube3
Posts: 30
Joined: Wed Mar 15, 2006 7:42 pm
Location: http://cube3.helpmy.net

Post 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.
Platform 3D Engine using Irrlicht.NET
http://cube3.helpmy.net
RapchikProgrammer
Posts: 279
Joined: Fri Dec 24, 2004 6:37 pm

Post 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
Hellfish
Posts: 3
Joined: Wed May 10, 2006 12:25 pm
Location: Sweden

Post 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.
Lordi in Eurovision Song Contest 06? Now we're getting somewhere!
Cube3
Posts: 30
Joined: Wed Mar 15, 2006 7:42 pm
Location: http://cube3.helpmy.net

Post 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
Platform 3D Engine using Irrlicht.NET
http://cube3.helpmy.net
Hellfish
Posts: 3
Joined: Wed May 10, 2006 12:25 pm
Location: Sweden

Post by Hellfish »

Thanks, that did the trick!

Now I'll just have to put some thought into understanding it. :)
Lordi in Eurovision Song Contest 06? Now we're getting somewhere!
forrestcupp
Posts: 5
Joined: Fri Nov 03, 2006 8:16 pm

Post 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!
Cube3
Posts: 30
Joined: Wed Mar 15, 2006 7:42 pm
Location: http://cube3.helpmy.net

Post 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. ;)
Platform 3D Engine using Irrlicht.NET
http://cube3.helpmy.net
BLiTZWiNG
Posts: 8
Joined: Tue Jul 04, 2006 11:19 pm

Post by BLiTZWiNG »

Cube3 wrote: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
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.
jun
Posts: 12
Joined: Thu Dec 07, 2006 5:00 pm

Post 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;
}
}
}
Locked