bool keyboard input with c#

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
olli
Posts: 3
Joined: Fri Aug 22, 2003 6:44 am
Location: Germany

bool keyboard input with c#

Post by olli »

hi,

does anyone know how to implement the keyboard input in c# like: http://www.irrforge.org/index.php/Keyboard_Input

it would be a nice extension for the irrlicht wiki. ;)
theandrew80
Posts: 11
Joined: Tue Mar 07, 2006 8:02 am
Location: Italy

Post by theandrew80 »

Hi olli, the method is the same: derive a custom class from the interface IEventReceiver, like this:

Code: Select all

	class MyEventReceiver : IEventReceiver
	{
		public MyEventReceiver()
		{
		}

		public bool OnEvent(Irrlicht.Event e)
		{
			bool Handled = true;

			if( e.Type == EventType.KeyInput && !e.KeyPressedDown )
			{
				switch(e.Key)
				{
					case KeyCode.KEY_RETURN:
						// do something
						break;
					case KeyCode.KEY_SPACE:
						// do something else
						break;
					default:
						Handled = false;
					break;
				}
			}
			else
				Handled = false;

			return Handled;
		}

	}
Then create an istance of this class and pass it to the IrrlichtDevice, like this:

Code: Select all

			IrrlichtDevice m_Device = new IrrlichtDevice(DriverType.DIRECT3D9, new Dimension2D(1280, 1024), 16, true, true, true);
			
			MyEventReceiver Receiver = new MyEventReceiver();

			m_Device.EventReceiver = Receiver;
olli
Posts: 3
Joined: Fri Aug 22, 2003 6:44 am
Location: Germany

Post by olli »

hi theandrew80, thanks for the post but i seek for input method to perform smooth movement. i search for somthing like this in c#:

init:

Code: Select all

bool keys[irr::KEY_KEY_CODES_COUNT];

for(int x=0; x<irr::KEY_KEY_CODES_COUNT; x++)
  keys[x] = false;
OnEvent():

Code: Select all

if(event.EventType == irr::EET_KEY_INPUT_EVENT)
{
     keys[event.KeyInput.Key] = event.KeyInput.PressedDown; 
     return true; 
} 
main gameloop:

Code: Select all

if(keys[irr::KEY_LEFT])
     pos->X -= 1; 
if(keys[irr::KEY_RIGHT])
     pos->X += 1; 
at the moment my solution is an evil hack :)
OnEvent():

Code: Select all

key = event.Key;
input = event.KeyPressedDown;
main gameloop:

Code: Select all

if (input)
{
     switch (key)
     {
          case KeyCode.KEY_ESCAPE:
          ...
olli
Posts: 3
Joined: Fri Aug 22, 2003 6:44 am
Location: Germany

Post by olli »

i solved the problem:

OnEvent():

Code: Select all

public bool OnEvent(Event eve)
{
  if (eve.Type == EventType.KeyInput)
  {
    keys[(int) eve.Key] = eve.KeyPressedDown;
    return true;
  }
  return false;
}
Init:

Code: Select all

keys = new bool[(int) KeyCode.KEY_KEY_CODES_COUNT];
gameloop:

Code: Select all

if (keys[(int) KeyCode.KEY_UP])
  do something...
Locked