C# - Detect Keyboard 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
geronika2004
Posts: 11
Joined: Mon Jan 22, 2007 5:42 pm
Location: Tbilisi, Georgia, Eastern Europe
Contact:

C# - Detect Keyboard Input

Post by geronika2004 »

:( Please, help me. I looked through entire forum and I still have a problem with keyboard. Now my code complains: "Object reference not set to an instance of an object".








using System;
using Irrlicht;
using Irrlicht.Video;
using Irrlicht.Core;
using Irrlicht.Scene;

namespace HelloWorld
{
// static IrrlichtDevice device = null;

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:

break;
case KeyCode.KEY_SPACE:
// do something else
break;
default:
Handled = false;
break;
}
}
else
Handled = false;

return Handled;
}

}

class rrr
{
[STAThread]
static void Main()
{
IrrlichtDevice device = new IrrlichtDevice(DriverType.DIRECT3D9, new Dimension2D(800, 600), 16, false, true, true);
MyEventReceiver Receiver = new MyEventReceiver();

device.EventReceiver = Receiver;


ITexture tex =device.VideoDriver.GetTexture(@"1.jpg");
Irrlicht.Scene.IAnimatedMesh mesh = device.SceneManager.GetMesh(@"1.x");
ICameraSceneNode cam = device.SceneManager.AddCameraSceneNodeFPS(null, 100, 100, -1);
cam.Position = new Vector3D(20,0,-50);
ISceneNode node = device.SceneManager.AddAnimatedMeshSceneNode(mesh, null, -1);
node.SetMaterialTexture(0, tex);
node.SetMaterialFlag(MaterialFlag.LIGHTING, false);
device.CursorControl.Visible = false;

while(device.Run())
{
if (device.WindowActive)
{
device.VideoDriver.BeginScene(true, true, new Color(0,100,100,100));
device.SceneManager.DrawAll();
device.VideoDriver.EndScene();
device.WindowCaption=EventType.LogText.ToString();
}
} // end drawing-loop
} // end main()
} // end class
} // end namespace
Kiwsa
Posts: 2
Joined: Fri Jan 12, 2007 5:46 pm
Location: bfe, USA

using a form in vb2005

Post by Kiwsa »

I got it to work in vb2005 using a form's class by

Code: Select all

implements Ieventreceiver


and building the proxy function:

Code: Select all

    Public Function OnEvent(ByVal e As Irrlicht.Event) As Boolean Implements Irrlicht.IEventReceiver.OnEvent
        If e.Type = EventType.KeyInput And e.Key = KeyCode.KEY_ESCAPE Then
            shutdown = True
        End If
    End Function
as part of the form's class....then all I needed to do was set the device.eventreceiver to point to the form..... But then again I am barely competent at reading c# code, but it apears that you also declared a new ieventreceiver, which I did not.

anyway...Hope this is SOME help at least, because it was VERY frustrating to me too.

Kiwsa
geronika2004
Posts: 11
Joined: Mon Jan 22, 2007 5:42 pm
Location: Tbilisi, Georgia, Eastern Europe
Contact:

Post by geronika2004 »

Kiwsa, Thank you, but I still have problem. I removed all unnecessary code and this code works, I mean sintax is correct, but keyboard still doesn't work :( :( :(


using System;
using Irrlicht;
using Irrlicht.Video;
using Irrlicht.Core;
using Irrlicht.Scene;

namespace HelloWorld
{
class Example : IEventReceiver
{
public bool OnEvent(Irrlicht.Event e )
{
if (e.Type == EventType.KeyInput)
{
Environment.Exit(0) ;
return true;
}
return false;
}

static void Main(string[] args)
{
IrrlichtDevice device = new IrrlichtDevice(DriverType.OPENGL,new Dimension2D(800, 600),32,false,true,true);
ITexture texSydney = device.VideoDriver.GetTexture(@"1.jpg");
Irrlicht.Scene.IAnimatedMesh mesh = device.SceneManager.GetMesh(@"1.x");
ICameraSceneNode cam = device.SceneManager.AddCameraSceneNodeFPS(null, 100, 100, -1);
cam.Position = new Vector3D(20,0,-150);
ISceneNode node = device.SceneManager.AddAnimatedMeshSceneNode(mesh, null, -1);
node.SetMaterialTexture(0, texSydney);
node.SetMaterialFlag(MaterialFlag.LIGHTING, false);

while(device.Run())
{
if (device.WindowActive)
{
device.VideoDriver.BeginScene(true, true, new Color(0,100,100,100));
device.SceneManager.DrawAll();
device.VideoDriver.EndScene();
}
} // end drawing-loop
} // end main()
} // end class
} // end namespace
matiae
Posts: 16
Joined: Wed May 31, 2006 8:06 am
Location: Chile

Post by matiae »

Hi,
You missed to set the EventReceiver, which is the same class.. so you should do device.EventReceiver = this but you can't because main is a static method :wink:
geronika2004
Posts: 11
Joined: Mon Jan 22, 2007 5:42 pm
Location: Tbilisi, Georgia, Eastern Europe
Contact:

Post by geronika2004 »

I tried everything, but I still can't make it work :( :( :(
Please, can anybody rewrite this code to me ??? I will appreciate deeply.

Code: Select all

using System; 
using Irrlicht; 
using Irrlicht.Video; 
using Irrlicht.Core; 
using Irrlicht.Scene; 

namespace HelloWorld 
{ 
	public class MyEventReceiver : IEventReceiver 
	{ 
		//public MyEventReceiver() //constructor
		//{ 
		//} 
		
		public bool OnEvent(Irrlicht.Event e ) 
		{ 
			if (e.Type == EventType.KeyInput) 
			{ 
				Environment.Exit(0) ; 
				return true; 
			} 
			return false; 
		} 

		public void run()
		{
			
			IrrlichtDevice device = new IrrlichtDevice(DriverType.DIRECT3D9, new Dimension2D(800, 600), 16, false, true, true); 
			device.EventReceiver =this; 


			ITexture tex =device.VideoDriver.GetTexture(@"1.jpg"); 
			Irrlicht.Scene.IAnimatedMesh mesh = device.SceneManager.GetMesh(@"1.x"); 
			ICameraSceneNode cam = device.SceneManager.AddCameraSceneNodeFPS(null, 100, 100, -1); 
			cam.Position = new Vector3D(20,0,-50); 
			ISceneNode node = device.SceneManager.AddAnimatedMeshSceneNode(mesh, null, -1); 
			node.SetMaterialTexture(0, tex); 
			node.SetMaterialFlag(MaterialFlag.LIGHTING, false); 
			device.CursorControl.Visible = false; 

			while(device.Run()) 
			{ 
				if (device.WindowActive) 
				{ 
					device.VideoDriver.BeginScene(true, true, new Color(0,100,100,100)); 
					device.SceneManager.DrawAll(); 
					device.VideoDriver.EndScene(); 
					device.WindowCaption=EventType.LogText.ToString(); 
				} 
			} // end drawing-loop 
		} // end run 
	} // end class 
	
	public class mainclass
	{
		[STAThread] 
		static void Main() 
		{ 
			MyEventReceiver zzz=new MyEventReceiver();
			zzz.run();

		}
	}

} // end namespace
geronika2004
Posts: 11
Joined: Mon Jan 22, 2007 5:42 pm
Location: Tbilisi, Georgia, Eastern Europe
Contact:

Post by geronika2004 »

my code says:

An unhandled exception of type 'System.NullReferenceException' occurred in irrlicht.net.dll

Additional information: Object reference not set to an instance of an object.
geronika2004
Posts: 11
Joined: Mon Jan 22, 2007 5:42 pm
Location: Tbilisi, Georgia, Eastern Europe
Contact:

Post by geronika2004 »

Eventually I have solved my problem, the reason of application failure was following:

I WAS USING VISUAL STUDIO 2003

I installed new version VISUAL STUDIO 2005 and now everything works :)

Thanks everybody :wink:
Locked