Irrlicht Keypresses

1.7k Views Asked by At

I am struggling to program keypresses within Irrlicht.

I have created an eventreciever as such:

class MyEventReceiver : public IEventReceiver
{
    virtual bool OnEvent(const SEvent& event)
    {
        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.PressedDown)
                    switch(event.KeyInput.Key)
                    {
                        case KEY_KEY_1:
                        case KEY_KEY_2:
                        case KEY_KEY_3:
                    }
                return true;
            }
        }
         return false;
    }

virtual bool IsKeyDown(EKEY_CODE keyCode) const
{
    return KeyIsDown[keyCode];
}

MyEventReceiver()
{
    memset(KeyIsDown, false, sizeof(KeyIsDown));
}

private:
    bool KeyIsDown[KEY_KEY_CODES_COUNT];
};

This all seems to be working as such. However, within "while(device->run())" I have implemented:

if(receiver.IsKeyDown(irr::KEY_KEY_1))
{

}

to which I get an error for my reciever "identifier reciever is undefined". On all examples I see, I see this reciever variable with no declarations and they claim it works. What am I doing wrong?

I am building on the example project "LoadIrrFile" (#15).

The plan is to implement a weapon switch for keys 1-3. I should be able to get the code in once I have the keypress initialised.

I am using the snippet I found here: http://irrlicht.sourceforge.net/forum//viewtopic.php?p=143082

Here is a full code segment if more info is required: http://pastie.org/pastes/8620301/text

1

There are 1 best solutions below

1
On

The snippet there is only a patch. receiver is not declared in your main(). Have a look at the "complete" example at http://irrlicht.sourceforge.net/docu/example004.html. Your code lacks something along (from the example in the link above):

MyEventReceiver receiver; // declare it
IrrlichtDevice* device = createDevice(driverType,
        core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver); // use it here