Page 1 of 1

Problem with key is pressed event

Posted: Tue Jun 26, 2007 4:00 pm
by alfabeta90
How to make a key is pressed event?
I make this one but when i run the program, program crach out.

Code: Select all

IrrlichtDevice *device = 0;
s32 cnt = 0;


class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
			s32 id = event.GUIEvent.Caller->getID();
			IGUIEnvironment* env = device->getGUIEnvironment();

		if (event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.Key == KEY_SPACE && event.KeyInput.PressedDown == false)
		{
			MessageBox(0, "asd", "ad", MB_OK);


		}

		return true;		
	}
};

Posted: Tue Jun 26, 2007 4:18 pm
by JonLT
did you assign the event receiver to the irrlicht device?

Code: Select all

device->setEvenreceiver(&myEventReceiver);
You could also assign the event receiver when you create the irrlicht device, if i remember correctly ist the last parameter.

Posted: Tue Jun 26, 2007 4:36 pm
by alfabeta90
Yes

Posted: Tue Jun 26, 2007 4:39 pm
by vitek
I'd be willing to bet that this is the classic global variable problem.

Code: Select all

IrrlichtDevice* device = 0; // 1

// snip

class MyEventReceiver : public IEventReceiver
{
public:
  virtual bool OnEvent(SEvent event)
  {
    // snip

    IGUIEnvironment* env = device->getGUIEnvironment(); // 2

    // snip
  }
};

int main()
{
  IrrlichtDevice* device = createDevice(...); // 3

  // snip
}
The device pointer at // 3 is initialized after main starts, but this has no effect on the global one at // 1. That device pointer is set to NULL before main starts to execute. When the program gets to // 2 the global device pointer is still NULL, and a function gets invoked on a NULL pointer.

Travis

Posted: Tue Jun 26, 2007 4:59 pm
by alfabeta90
So, what must i do?

Posted: Tue Jun 26, 2007 5:42 pm
by JonLT
you have two options: (that i can think of)
1) create the device in global scope
2) have the device as a memeber of the event receiver class