Page 1 of 1

a little "sweet snippet"

Posted: Fri Oct 31, 2003 5:11 am
by roninmagus
Howdy.. I'm new to Irrlicht. well, kind of.. I started messing with it at version 0.1, but I had to quit due to real life issues.

I yesterday picked up 0.4, and man am I impressed. It's come a long way.

Anyways, I was reading through the tutorials and I was wondering how to make the program exit when the user presses Escape. This is a simple solution I came up with. Let me know what you think, or if there are any problems!

Code: Select all

class InputQuitOnEscape : public IEventReceiver
{
public:
	InputQuitOnEscape() : IEventReceiver(), device(NULL)
	{
	}
	
	void SetDevice(IrrlichtDevice * d)
	{
		device = d;
	}
	
	virtual bool OnEvent(SEvent event)
	{
		if(device) {
			if(event.EventType == EET_KEY_INPUT_EVENT)  {
				if(event.KeyInput.Key == KEY_ESCAPE) {
					device->closeDevice();


					return true;
				}	
			}
		}

		return false;
	}

private:
	IrrlichtDevice * device;
};

The idea is to create the input receiver, then give it a pointer to the IrrlichtDevice, so it'll be able to close it when escape is pressed.

Posted: Fri Oct 31, 2003 12:41 pm
by saigumi
Yep, this would work. The way it's done in the TechDemo is almost the same, it just adds the check to perform the action when the user lets go of the ESC key.

Code: Select all

	if (event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown == false) {
		// user wants to quit.
		device->closeDevice();
	}