Key handling (Shift key)

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.
xaddict
Posts: 23
Joined: Tue Jan 29, 2008 7:18 pm

Key handling (Shift key)

Post by xaddict »

There are some topics on here which handle the topic but I don't really get them... it's hard to be new with this :P

Okay.. so I'm trying to printf "Shift Key pressed" to the console when the shift key is pressed.
Which function do I use to check if a key is pressed, which files should I include at the header and so on?

can someone give me a working piece of code to do this?

I use:

Code: Select all

bool OnEvent(const SEvent& event)
	{
		// check if user presses the key 'E' or 'R'
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
		{
			if (event.KeyInput.Key == irr::KEY_KEY_E)
				printf("E");
			else
			if (event.KeyInput.Key == irr::KEY_KEY_R)
				printf("R");
			else
				return false;
		}

		return false;
	}
and put it in front of int main()

but when I press E or R it doesn't do anything.. where should I check for the onEvent thingie or what should I add to my main() function?
Last edited by xaddict on Fri Feb 01, 2008 3:54 pm, edited 1 time in total.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Check the wiki (link on the main irrlicht page) and search for MastEventReceiver.

That gives you the source for an event receiver (the way we detect key presses, mouse presses & movements and gui events) and should be clear how to tell if the shift button is pressed.
Image Image Image
xaddict
Posts: 23
Joined: Tue Jan 29, 2008 7:18 pm

Post by xaddict »

How do I include MastEventReceiver?

do I just #include<MastEventReceiver.cpp> and then I'm able to use the functions?
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

you should do the tutorials, they show how to use an event receiver... :roll:
for example the tutorials #04.Movement and #05.UserInterface... ;)

in short: you'll need to tell the Irrlicht device to use the event receiver, just putting him in front of the function main doesn't work !!! :lol:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Heheh. That function should be a method of an IEventReceiver derived class. See example 04.Movement in the SDK for more details.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
xaddict
Posts: 23
Joined: Tue Jan 29, 2008 7:18 pm

Post by xaddict »

I've put in all the code from the example but it doesn't work:

Code: Select all

/*
In this tutorial, I will show how to collision detection with the Irrlicht Engine. 
I will describe 3 methods: Automatic collision detection for moving through 3d worlds
with stair climbing and sliding, manual triangle picking and manual
scene node picking.

To start, we take the program from tutorial 2, which loaded and displayed a quake 3
level. We will use the level to walk in it and to pick triangles from it. In addition
we'll place 3 animated models into it for scene node picking. The following code 
starts up the engine and loads
a quake 3 level. I will not explain it, because it should already be known from tutorial
2.
*/
#include <irrlicht.h>
#include <iostream>


using namespace irr;

#pragma comment(lib, "Irrlicht.lib")
scene::ISceneNode* node = 0;

class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(const SEvent& event)
	{
		/*
		If the key 'W' or 'S' was left up, we get the position of the scene node,
		and modify the Y coordinate a little bit. So if you press 'W', the node
		moves up, and if you press 'S' it moves down.
		*/

		if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT&&
			!event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_KEY_E:
			case KEY_KEY_R:
				{
					core::vector3df v = node->getPosition();
					v.Y += event.KeyInput.Key == KEY_KEY_R ? 2.0f : -2.0f;
					node->setPosition(v);
				}
				return true;
			}
		}

		return false;
	}
};
int main()
{
	// let user select driver type

	video::E_DRIVER_TYPE driverType;

	printf("Please select the driver you want for this example:\n"\
		" (a) Direct3D 9.0c\n");

	char i;
	std::cin >> i;

	switch(i)
	{
		case 'a': driverType = video::EDT_DIRECT3D9;break;

		default: driverType = video::EDT_DIRECT3D9;
	}

	// create device

	IrrlichtDevice *device =
		createDevice(driverType, core::dimension2d<s32>(800, 600), 16, false);
		
	if (device == 0)
		return 1; // could not create selected driver.

	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();

	
	device->getFileSystem()->addZipFileArchive("../../media/map-20kdm2.pk3");

	
	scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
	scene::ISceneNode* q3node = 0;
	
	if (q3levelmesh)
		q3node = smgr->addOctTreeSceneNode(q3levelmesh->getMesh(0));

	/*
	So far so good, we've loaded the quake 3 level like in tutorial 2. Now, here
	comes something different: We create a triangle selector. A triangle selector
	is a class which can fetch the triangles from scene nodes for doing different
	things with them, for example collision detection. There are different triangle
	selectors, and all can be created with the ISceneManager. In this example,
	we create an OctTreeTriangleSelector, which optimizes the triangle output a l
	little bit by reducing it like an octree. This is very useful for huge meshes
	like quake 3 levels.
	Afte we created the triangle selector, we attach it to the q3node. This is not
	necessary, but in this way, we do not need to care for the selector, for example
	dropping it after we do not need it anymore.
	*/

	scene::ITriangleSelector* selector = 0;
	
	if (q3node)
	{		
		q3node->setPosition(core::vector3df(-1350,-130,-1400));

		selector = smgr->createOctTreeTriangleSelector(q3levelmesh->getMesh(0), q3node, 128);
		q3node->setTriangleSelector(selector);
	}


	//add a node

	node = smgr->addSphereSceneNode();
	node->setPosition(core::vector3df(-100,50,-150));
	node->setMaterialFlag(video::EMF_LIGHTING, false);
	
	
	/*
	We add a first person shooter camera to the scene for being able to move in the quake 3
	level like in tutorial 2. But this, time, we add a special animator to the 
	camera: A Collision Response animator. This thing modifies the scene node to which
	it is attached to in that way, that it may no more move through walls and is affected
	by gravity. The only thing we have to tell the animator is how the world looks like,
	how big the scene node is, how gravity and so on. After the collision response animator
	is attached to the camera, we do not have to do anything more for collision detection,
	anything is done automaticly, all other collision detection code below is for picking.
	And please note another cool feature: The collsion response animator can be attached
	also to all other scene nodes, not only to cameras. And it can be mixed with other
	scene node animators. In this way, collision detection and response in the Irrlicht
	engine is really, really easy.
	Now we'll take a closer look on the parameters of createCollisionResponseAnimator().
	The first parameter is the TriangleSelector, which specifies how the world, against
	collision detection is done looks like. The second parameter is the scene node, which
	is the object, which is affected by collision detection, in our case it is the camera.
	The third defines how big the object is, it is the radius of an ellipsoid. Try it out 
	and change the radius to smaller values, the camera will be able to move closer to walls
	after this. The next parameter is the direction and speed of gravity. You could
	set it to (0,0,0) to disable gravity. And the last value is just a translation: Without
	this, the ellipsoid with which collision detection is done would be around the camera,
	and the camera would be in the middle of the ellipsoid. But as human beings, we are 
	used to have our eyes on top of the body, with which we collide with our world, not
	in the middle of it. So we place the scene node 50 units over the center of the 
	ellipsoid with this parameter. And that's it, collision detection works now. 
	*/
	SKeyMap keyMap[5];
                 keyMap[0].Action = EKA_MOVE_FORWARD;
                 keyMap[0].KeyCode = KEY_KEY_W;

                 keyMap[1].Action = EKA_MOVE_BACKWARD;
                 keyMap[1].KeyCode = KEY_KEY_S;

                 keyMap[2].Action = EKA_STRAFE_LEFT;
                 keyMap[2].KeyCode = KEY_KEY_A;

                 keyMap[3].Action = EKA_STRAFE_RIGHT;
                 keyMap[3].KeyCode = KEY_KEY_D;

				 keyMap[4].Action = EKA_JUMP_UP;
                 keyMap[4].KeyCode = KEY_SPACE;

	scene::ICameraSceneNode* camera = 
		smgr->addCameraSceneNodeFPS(0, 100.0f, 300.0f, -1, keyMap, 5, true, 0.8f);
	camera->setPosition(core::vector3df(-100,50,-150));
	//core::position2d<s32> point = device->getCursorControl()->getPosition();
	if (selector)
	{
		//scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
		//	selector, camera, core::vector3df(30,50,30),
		//	core::vector3df(0,-3,0), 
		//	core::vector3df(0,50,0));
		scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
			selector, camera, core::vector3df(30,50,30),
			core::vector3df(0,-3,0), 
			core::vector3df(0,50,0));
		selector->drop();
		camera->addAnimator(anim);
		anim->drop();
	}

	/*
	Because collision detection is no big deal in irrlicht, I'll describe how to
	do two different types of picking in the next section. But before this,
	I'll prepare the scene a little. I need three animated characters which we 
	could pick later, a dynamic light for lighting them,
	a billboard for drawing where we found an intersection,	and, yes, I need to
	get rid of this mouse cursor. :)
	*/

	// disable mouse cursor

	device->getCursorControl()->setVisible(false);

	// add billboard

	scene::IBillboardSceneNode * bill = smgr->addBillboardSceneNode();
	bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR );
	bill->setMaterialTexture(0, driver->getTexture("../../media/particle.bmp"));
	bill->setMaterialFlag(video::EMF_LIGHTING, false);
	bill->setMaterialFlag(video::EMF_ZBUFFER, false);
	bill->setSize(core::dimension2d<f32>(37.7f,20.0f));

	// add 3 animated faeries.

	video::SMaterial material;
	material.setTexture(0, driver->getTexture("../../media/faerie2.bmp"));
	material.Lighting = true;

	scene::IAnimatedMeshSceneNode* node = 0;
	scene::IAnimatedMesh* faerie = smgr->getMesh("../../media/faerie.md2");

	material.setTexture(0, 0);
	material.Lighting = false;

	// Add a light

	smgr->addLightSceneNode(0, core::vector3df(-60,100,400),video::SColorf(1.0f,1.0f,1.0f,1.0f),600.0f);


	/*
	For not making it to complicated, I'm doing picking inside the drawing loop.
	We take two pointers for storing the current and the last selected scene node and 
	start the loop.
	*/


	scene::ISceneNode* selectedSceneNode = 0;
	scene::ISceneNode* lastSelectedSceneNode = 0;

	
	int lastFPS = -1;

	while(device->run())
	if (device->isWindowActive())
	{
		driver->beginScene(true, true, 0);

		smgr->drawAll();

		/*
		After we've drawn the whole scene whit smgr->drawAll(), we'll do the first
		picking: We want to know which triangle of the world we are looking at. In addition,
		we want the exact point of the quake 3 level we are looking at.
		For this, we create a 3d line starting at the position of the camera and going 
		through the lookAt-target of it. Then we ask the collision manager if this line
		collides with a triangle of the world stored in the triangle selector. If yes,
		we draw the 3d triangle and set the position of the billboard to the intersection 
		point.
		*/

		core::line3d<f32> line;
		line.start = camera->getPosition();
		line.end = line.start + (camera->getTarget() - line.start).normalize() * 1000.0f;

		core::vector3df intersection;
		core::triangle3df tri;

		if (smgr->getSceneCollisionManager()->getCollisionPoint(
			line, selector, intersection, tri))
		{
			bill->setPosition(intersection);
				
			driver->setTransform(video::ETS_WORLD, core::matrix4());
			driver->setMaterial(material);
			driver->draw3DTriangle(tri, video::SColor(0,255,0,0));
		}


		/*
		Another type of picking supported by the Irrlicht Engine is scene node picking
		based on bouding boxes. Every scene node has got a bounding box, and because of
		that, it's very fast for example to get the scene node which the camera looks
		at. Again, we ask the collision manager for this, and if we've got a scene node,
		we highlight it by disabling Lighting in its material, if it is not the 
		billboard or the quake 3 level.
		*/

		selectedSceneNode = smgr->getSceneCollisionManager()->getSceneNodeFromCameraBB(camera);

		if (lastSelectedSceneNode)
			lastSelectedSceneNode->setMaterialFlag(video::EMF_LIGHTING, true);

		if (selectedSceneNode == q3node || selectedSceneNode == bill)
			selectedSceneNode = 0;

		if (selectedSceneNode)
			selectedSceneNode->setMaterialFlag(video::EMF_LIGHTING, false);

		lastSelectedSceneNode = selectedSceneNode;


		/*
		That's it, we just have to finish drawing.
		*/

		driver->endScene();

		int fps = driver->getFPS();

		if (lastFPS != fps)
		{
		  core::stringw str = L"Tryout [";
		  str += driver->getName();
		  str += "] FPS:";
		  str += fps;

		  device->setWindowCaption(str.c_str());
		  lastFPS = fps;
		}
	}

	device->drop();
	
	return 0;
}

What am I doing wrong (except for trying things way over my head:P)
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Try this:

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
	// This is the one method that we have to implement
	virtual bool OnEvent(const SEvent& event)
	{
		// Remember whether each key is down or up
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
		{
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

			if(event.KeyInput.Shift)
				printf("Shift was pressed\n");
		}

		return false;
	}

	// This is used to check whether a key is being held down
	virtual bool IsKeyDown(EKEY_CODE keyCode) const
	{
		return KeyIsDown[keyCode];
	}

	MyEventReceiver()
	{
		for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
			KeyIsDown[i] = false;
	}

private:
	// We use this array to store the current state of each key
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

That won't work either really, not the 'if(event.KeyInput.Shift) printf("Shift was pressed\n");' bit anyway.

You need to check KeyIsDown[SHIFT_KEY_ENUM_DON'T_KNOW_WHICH] rather than just printing if the event was for shift as you'll get an event for shift when you let go too ;)
Image Image Image
xaddict
Posts: 23
Joined: Tue Jan 29, 2008 7:18 pm

Post by xaddict »

JP wrote:That won't work either really, not the 'if(event.KeyInput.Shift) printf("Shift was pressed\n");' bit anyway.

You need to check KeyIsDown[SHIFT_KEY_ENUM_DON'T_KNOW_WHICH] rather than just printing if the event was for shift as you'll get an event for shift when you let go too ;)
I've not gotten the code to work till now... nothing shows in the command console... should I use the class in main? or does it work by itself?
or do I have to use a function in main?
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

JP wrote:That won't work either really, not the 'if(event.KeyInput.Shift) printf("Shift was pressed\n");' bit anyway.

You need to check KeyIsDown[SHIFT_KEY_ENUM_DON'T_KNOW_WHICH] rather than just printing if the event was for shift as you'll get an event for shift when you let go too ;)
Er.. no. But it seems to "press" 3 times automatically before I pressed anything, haven't checked why though..

Well, then you could do this:

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
	// This is the one method that we have to implement
	virtual bool OnEvent(const SEvent& event)
	{
		// Remember whether each key is down or up
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

		return false;
	}

	// This is used to check whether a key is being held down
	virtual bool IsKeyDown(EKEY_CODE keyCode) const
	{
		return KeyIsDown[keyCode];
	}

	MyEventReceiver()
	{
		for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
			KeyIsDown[i] = false;
	}

private:
	// We use this array to store the current state of each key
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
In main():

Code: Select all

	MyEventReceiver receiver;

	IrrlichtDevice* device = createDevice( driverType, core::dimension2d<s32>(640, 480),
		16, false, false, false, &receiver);

	if (device == 0)
		return 1; // could not create selected driver.
In while:

Code: Select all

		if(receiver.IsKeyDown(irr::KEY_SHIFT))
		{
			printf("Shift key pressed\n");
			// Beware it checks if it's down, not clicked once!!
		}
Edit:
This will check only once - I've checked and it's working, JP..

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
	// This is the one method that we have to implement
	virtual bool OnEvent(const SEvent& event)
	{
		// Remember whether each key is down or up
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

		if (event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Shift)
			printf("Shift clicked once\n");

		return false;
	}

	// This is used to check whether a key is being held down
	virtual bool IsKeyDown(EKEY_CODE keyCode) const
	{
		return KeyIsDown[keyCode];
	}

	MyEventReceiver()
	{
		for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
			KeyIsDown[i] = false;
	}

private:
	// We use this array to store the current state of each key
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
With the latter event receiver you don't need any check in main or while or anywhere else..

Edit2: If it's any help here's the full .cpp file: 04.Movement

Code: Select all

/*
This Tutorial shows how to move and animate SceneNodes. The
basic concept of SceneNodeAnimators is shown as well as manual
movement of nodes using the keyboard.

As always, I include the header files, use the irr namespace,
and tell the linker to link with the .lib file.
*/
#include <irrlicht.h>
#include <iostream>

using namespace irr;

#pragma comment(lib, "Irrlicht.lib")

/*
To receive events like mouse and keyboard input, or GUI events like 
"the OK button has been clicked", we need an object which is derived from the 
IEventReceiver object. There is only one method to override: OnEvent. 
This method will be called by the engine once when an event happens. 
What we really want to know is whether a key is being held down,
and so we will remember the current state of each key.
*/
class MyEventReceiver : public IEventReceiver
{
public:
	// This is the one method that we have to implement
	virtual bool OnEvent(const SEvent& event)
	{
		// Remember whether each key is down or up
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

		if (event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.Shift)
			printf("Shift clicked once\n");

		return false;
	}

	// This is used to check whether a key is being held down
	virtual bool IsKeyDown(EKEY_CODE keyCode) const
	{
		return KeyIsDown[keyCode];
	}

	MyEventReceiver()
	{
		for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
			KeyIsDown[i] = false;
	}

private:
	// We use this array to store the current state of each key
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
};


/*
The event receiver for moving a scene node is ready. So lets just create
an Irrlicht Device and the scene node we want to move. We also create some
other additional scene nodes, to show that there are also some different 
possibilities to move and animate scene nodes.
*/
int main()
{
	// let user select driver type

	video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;

	printf("Please select the driver you want for this example:\n"\
		" (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"\
		" (d) Software Renderer\n (e) Burning's Software Renderer\n"\
		" (f) NullDevice\n (otherKey) exit\n\n");

	char i;
	std::cin >> i;

	switch(i)
	{
	case 'a': driverType = video::EDT_DIRECT3D9;break;
	case 'b': driverType = video::EDT_DIRECT3D8;break;
	case 'c': driverType = video::EDT_OPENGL;   break;
	case 'd': driverType = video::EDT_SOFTWARE; break;
	case 'e': driverType = video::EDT_BURNINGSVIDEO;break;
	case 'f': driverType = video::EDT_NULL;     break;
	default: return 0;
	}	

	// create device
	MyEventReceiver receiver;

	IrrlichtDevice* device = createDevice( driverType, core::dimension2d<s32>(640, 480),
		16, false, false, false, &receiver);

	if (device == 0)
		return 1; // could not create selected driver.


	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();


	/*
	Create the node for moving it with the 'W' and 'S' key. We create a
	sphere node, which is a built in geometry primitive. We place the node
	at (0,0,30) and assign a texture to it to let it look a little bit more
	interesting. Because we have no dynamic lights in this scene we disable
	lighting for each model (otherwise the models would be black).
	*/
	scene::ISceneNode * node = smgr->addSphereSceneNode();
	if (node)
	{
		node->setPosition(core::vector3df(0,0,30));
		node->setMaterialTexture(0, driver->getTexture("../../media/wall.bmp"));
		node->setMaterialFlag(video::EMF_LIGHTING, false);
	}


	/* 
	Now we create another node, moving using a scene node animator. Scene
	node animators modify scene nodes and can be attached to any scene node
	like mesh scene nodes, billboards, lights and even camera scene nodes.
	Scene node animators are not only able to modify the position of a
	scene node, they can also animate the textures of an object for
	example.  We create a cube scene node and attach a 'fly circle' scene
	node to it, letting this node fly around our sphere scene node.
	*/
	scene::ISceneNode* n = smgr->addCubeSceneNode();

	if (n)
	{
		n->setMaterialTexture(0, driver->getTexture("../../media/t351sml.jpg"));
		n->setMaterialFlag(video::EMF_LIGHTING, false);
		scene::ISceneNodeAnimator* anim =
			smgr->createFlyCircleAnimator(core::vector3df(0,0,30), 20.0f);
		if (anim)
		{
			n->addAnimator(anim);
			anim->drop();
		}
	}

	/*
	The last scene node we add to show possibilities of scene node animators is 
	a md2 model, which uses a 'fly straight' animator to run between to points.
	*/
	scene::IAnimatedMeshSceneNode* anms = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/sydney.md2"));

	if (anms)
	{
		scene::ISceneNodeAnimator* anim =
			smgr->createFlyStraightAnimator(core::vector3df(100,0,60), 
			core::vector3df(-100,0,60), 2500, true);
		if (anim)
		{
			anms->addAnimator(anim);
			anim->drop();
		}

		/*
		To make to model look right we set the frames between which the animation
		should loop, rotate the model around 180 degrees, and adjust the animation speed
		and the texture.
		To set the right animation (frames and speed), we would also be able to just
		call "anms->setMD2Animation(scene::EMAT_RUN)" for the 'run' animation 
		instead of "setFrameLoop" and "setAnimationSpeed",
		but this only works with MD2 animations, and so you know how to start other animations.
		but it a good advice to use not hardcoded frame-numbers...
		*/
		anms->setMaterialFlag(video::EMF_LIGHTING, false);

		anms->setFrameLoop(160, 183);
		anms->setAnimationSpeed(40);
		anms->setMD2Animation(scene::EMAT_RUN);

		anms->setRotation(core::vector3df(0,180.0f,0));
		anms->setMaterialTexture(0, driver->getTexture("../../media/sydney.bmp"));

	}


	/*
	To be able to look at and move around in this scene, we create a first
	person shooter style camera and make the mouse cursor invisible.
	*/
	scene::ICameraSceneNode * cam = smgr->addCameraSceneNodeFPS(0, 100.0f, 100.0f);
	device->getCursorControl()->setVisible(false);

	/*
	Add a colorful irrlicht logo
	*/
	device->getGUIEnvironment()->addImage(
		driver->getTexture("../../media/irrlichtlogoalpha2.tga"),
		core::position2d<s32>(10,10));

	/*
	We have done everything, so lets draw it. We also write the current
	frames per second and the name of the driver to the caption of the
	window.
	*/
	int lastFPS = -1;

	while(device->run())
	{
		/* Check if key W or key S is being held down, and move the
		sphere node up or down respectively.
		*/
		if(receiver.IsKeyDown(irr::KEY_KEY_W))
		{
			core::vector3df v = node->getPosition();
			v.Y += 0.02f;
			node->setPosition(v);
		}
		else if(receiver.IsKeyDown(irr::KEY_KEY_S))
		{
			core::vector3df v = node->getPosition();
			v.Y -= 0.02f;
			node->setPosition(v);
		}

		/*if(receiver.IsKeyDown(irr::KEY_SHIFT))
		{
		printf("Shift key pressed\n");
		// Beware it checks if it's down, not clicked once!!
		}*/

		driver->beginScene(true, true, video::SColor(255,113,113,133));

		smgr->drawAll(); // draw the 3d scene
		device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)

		driver->endScene();

		int fps = driver->getFPS();

		if (lastFPS != fps)
		{
			core::stringw tmp(L"Movement Example - Irrlicht Engine [");
			tmp += driver->getName();
			tmp += L"] fps: "; 
			tmp += fps;

			device->setWindowCaption(tmp.c_str());
			lastFPS = fps;
		}
	}

	/*
	In the end, delete the Irrlicht device.
	*/
	device->drop();

	return 0;
}
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

You just need to create an instance of your event receiver, and tell Irrlicht to use it, by passing it as a parameter to your createDevice() call.

Code: Select all

	MyEventReceiver receiver;
	IrrlichtDevice *device =
		createDevice(driverType, core::dimension2d<s32>(800, 600), 16, false, false, false, &receiver);
Bingo, events. Note that what you're doing is handling key release events, so they'll only trigger each time you release key E or key R.

To answer your original question, shift is available both as a modifier, and as a key event by itself. It looks like you want to trap the key event, so this will do what you asked:

Code: Select all

   virtual bool OnEvent(const SEvent& event)
   {
      if (irr::EET_KEY_INPUT_EVENT == event.EventType &&
		  event.KeyInput.PressedDown &&
		  KEY_SHIFT == event.KeyInput.Key)
      {
		  (void)printf("Shift pressed down\n");
      }

      return false;
   }
That may not do what you want though. If you want to store whether shift is down, rather than each time it was pressed, then you'll have to store the state, as MasterGod suggested, then check the value of IsKeyDown(irr::KEY_SHIFT) from your application code.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

I've edited my post, it now has both ways.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
xaddict
Posts: 23
Joined: Tue Jan 29, 2008 7:18 pm

Post by xaddict »

MasterGod wrote:I've edited my post, it now has both ways.
Thank you both soo much :D it works now... it prints hundreds and hundreds of "Shift key is pressed" so I think that is good:P

Now I've got the next problem... the reason why I wanted to handle this SHIFT key was because I want to accelerate the camera movement...
Only problem is the camera has been defined before the drawing loop begins... is there a way to change the speed of the camera WHILE using it?

I've looked through the class reference for ICameraSceneNode but it doesn't have any helpful functions or am I missing something?
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

no, you can't, except with my IrrExtensions... ;)


btw, could it be that I pointed you to all this with my first post ??? :lol:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

I haven't checked why Acki's did in his project but currently you can't change a camera's speed with something like camera->setSpeed().
You'll have to manually change it with settings it's position etc..
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Post Reply