Pressing ESC to close the application.

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Pressing ESC to close the application.

Post by Mel »

Someone asked me :) I hope you find it useful. It is a simple event receiver class that exits the program when the key ESC is pressed. it can be mixed with more complete event receivers easily, i guess. It simply calls the runtime to exit the program inmediately

Code: Select all

 
class escEventReceiver : public irr::IEventReceiver
{
    virtual bool OnEvent (const irr::SEvent &event)
    {
        if(event.EventType == irr::EET_KEY_INPUT_EVENT)
        {
            if(event.KeyInput.Key == irr::KEY_ESCAPE)
            {
                exit(0);//We exit 
            }
        }
        return false;
    }
};
 
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Re: Pressing ESC to close the application.

Post by greenya »

Why not closeDevice() and allow execution of some code after while-device-run loop? :)
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Pressing ESC to close the application.

Post by Mel »

The point is to properly check the pressing of the Esc key, it is intended to be directly used, as the exit call frees all the user memory, closes all the opened files and so on, is a quick way to end the program, and seems that all the underlying systems have their own way to handle the exit call. So it is safe. Anyway it can be modified to call closeDevice().
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Pressing ESC to close the application.

Post by The_Glitch »

It doesn't seem to do anything for me.
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Pressing ESC to close the application.

Post by Mel »

Well, it is pretty straightforward... Have you set the event receiver in the device?
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Pressing ESC to close the application.

Post by The_Glitch »

escEventReceiver* receiver;
receiver = new escEventReceiver();

after device is created or defined in your code post
device->setEventReceiver(receiver);

Thanks Mel.
Post Reply