Multiple Key Board Keys

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
krama757
Posts: 451
Joined: Sun Nov 06, 2005 12:07 am

Multiple Key Board Keys

Post by krama757 »

Hmm, Im trying to make a custom camera scene node and I think I found a prob with the engine...

Is there anyway to take input from two keys like W & A? From what I can see the engine only allows one key to be pressed at a time when using the IEvenetReceiver class.
Conquistador
Posts: 340
Joined: Wed Sep 28, 2005 4:38 pm
Location: Canada, Eh!

re:

Post by Conquistador »

Here's an event receiver class that I use, every time a key is pressed, a key in an array gets set to true, and you can handle the keypresses in the main loop:

CEventReceiver.h

Code: Select all

// Event Receiver Class
class CEventReceiver : public IEventReceiver
{
   public:
      CEventReceiver();
      virtual bool OnEvent(SEvent Event);
      bool getKeyState(EKEY_CODE key);
   private:
      bool keys[KEY_KEY_CODES_COUNT];
};
CEventReceiver.cpp

Code: Select all

// Include headers
#include "include/CEventReceiver.h"

// Constructor
CEventReceiver::CEventReceiver()
{
   for (int i = 0; i < KEY_KEY_CODES_COUNT; i++)
   {
      keys[i] = false;
   }   
}

// Event handler function
bool CEventReceiver::OnEvent(SEvent Event)
{
   if (Event.EventType == EET_KEY_INPUT_EVENT)
   {
      keys[Event.KeyInput.Key] = Event.KeyInput.PressedDown;
   }
   
   return true;
}

// Get key state
bool CEventReceiver::getKeyState(EKEY_CODE key)
{
   return keys[key];   
}

In your main loop, handle keypresses like so:

Code: Select all

if (EventReceiver->getKeyState(KEY_KEY_A))
{
    // Do something
}
[/code]
krama757
Posts: 451
Joined: Sun Nov 06, 2005 12:07 am

Post by krama757 »

Thats a nice way of doing it. I thought it was a prob with the engine itself...thanks a bunch.

However, does your FPS camera work?
MikeR
Posts: 767
Joined: Sun Dec 26, 2004 4:03 pm
Location: Northern California USA
Contact:

Post by MikeR »

Is there anyway to take input from two keys like W & A?
Yes.

Code: Select all

if (EventReceiver->getKeyState(KEY_KEY_A && KEY_KEY_W)) 
{ 
    // Do something 
} 
If it exists in the real world, it can be created in 3d

Sys specs:
AMD 3700+ 64 processor
1.0 gb ram
e-Geforce 6600 graphics 256 mb onboard ram
Conquistador
Posts: 340
Joined: Wed Sep 28, 2005 4:38 pm
Location: Canada, Eh!

Post by Conquistador »

However, does your FPS camera work?
Actually, no it doesn't, for me anyways. I'm gonna take a look at that sometime later. If you want, maybe try this:

Code: Select all

// Event handler function
bool CEventReceiver::OnEvent(SEvent Event)
{
   if (Event.EventType == EET_KEY_INPUT_EVENT)
   {
      keys[Event.KeyInput.Key] = Event.KeyInput.PressedDown;
      return true;
   }
   
   return false;
} 
r3i
Posts: 147
Joined: Wed Jun 29, 2005 10:15 am
Location: Sorrento
Contact:

Post by r3i »

Sorry....a curiosity: and if I want to detect the click of due the mouse buttons??
Is correct try with "getKeyState" ?
"We work in the dark, we do what we can, we give what we have, Our doubt is our passion and our passion is our task. The rest: is art of Madness" (H.James)
Conquistador
Posts: 340
Joined: Wed Sep 28, 2005 4:38 pm
Location: Canada, Eh!

re:

Post by Conquistador »

No, you'd have to do basically the same thing I did with the keyboard, except make an array of mouse keys, and every time a mouse button is down, set a key in the array to true, every time it is lifted, set the array key back to false. If you want to do that, you should make the method return a position2d<> with the coords of the last mouse click.
r3i
Posts: 147
Joined: Wed Jun 29, 2005 10:15 am
Location: Sorrento
Contact:

Post by r3i »

Ok thank ou,
I don't know but i've problems because I've get to left button and right button some actions... i must try: thank you! ^.^
"We work in the dark, we do what we can, we give what we have, Our doubt is our passion and our passion is our task. The rest: is art of Madness" (H.James)
krama757
Posts: 451
Joined: Sun Nov 06, 2005 12:07 am

Post by krama757 »

Yep, already tried that Conquistador, doesnt seem to help. I was trying various other methods to no avail.

Mike, thanks, but already knew that lol :D
SARIN
Posts: 139
Joined: Fri Oct 29, 2004 3:53 am

Post by SARIN »

Post Reply