Page 1 of 1
bool keyboard input with c#
Posted: Tue Mar 07, 2006 5:39 pm
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.
Posted: Wed Mar 08, 2006 8:03 am
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;
Posted: Wed Mar 08, 2006 10:02 am
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:
...
Posted: Wed Mar 08, 2006 10:27 am
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...