Key Press Detection Help

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.
Post Reply
elkar
Posts: 8
Joined: Tue Jan 13, 2009 10:02 pm

Key Press Detection Help

Post by elkar »

First of all I have to say that I'm relatively new to using Irrlicht, so excuse me is the answer to my question is super obvious. I have been following the "Movement" tutorial for Irrlicht, and trying to hook the key press function up to my own program. There aren't any errors or warnings when compiling, but the program doesn't seem to detect any of my key presses. My code is as follows:

Code: Select all

#include <irrlicht.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

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 == 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];
};

int main(int argc, char** argv)
{
    MyEventReceiver receiver;

    //Create the main device for the game
    IrrlichtDevice *device =
        createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 16,
            false, false, false, 0);

    //Set the window caption
    device->setWindowCaption(L"Psychic Demo");

    //Create all of the environments for the game
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();

    //Create the head
    ISceneNode* head = smgr->addSphereSceneNode();

    //Create the body
    ISceneNode* body = smgr->addSphereSceneNode();

    //Create one hand
    ISceneNode* hand1 = smgr->addSphereSceneNode();

    //Create the other hand
    ISceneNode* hand2 = smgr->addSphereSceneNode();

    //Create one foot
    ISceneNode* foot1 = smgr->addSphereSceneNode();

    //Create the other foot
    ISceneNode* foot2 = smgr->addSphereSceneNode();

    //Test cube
    ISceneNode* cube = smgr->addCubeSceneNode();
    cube->setPosition(vector3df(30, 0, 0));

    //Set stuff for head
    if (head)
    {
        head->setMaterialFlag(EMF_LIGHTING, true);
        head->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
        head->setScale(vector3df(0.8, 0.8, 0.8));
        head->setPosition(vector3df(0,0,0));
        head->setDebugDataVisible(EDS_FULL);
    }

    //Set stuff for the body
    if (body)
    {
        body->setParent(head);
        body->setMaterialFlag(EMF_LIGHTING, true);
        body->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
        body->setScale(vector3df(1, 1.8, 1));
        body->setPosition(head->getPosition() + vector3df(0, -15, 0));
    }

    //Stuff for hand1
    if (hand1)
    {
        hand1->setParent(head);
        hand1->setMaterialFlag(EMF_LIGHTING, true);
        hand1->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
        hand1->setScale(vector3df(0.5, 0.5, 0.5));
        hand1->setPosition(head->getPosition() + vector3df(0, -16, -8));
    }

    //Stuff for hand2
    if (hand2)
    {
        hand2->setParent(head);
        hand2->setMaterialFlag(EMF_LIGHTING, true);
        hand2->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
        hand2->setScale(vector3df(0.5, 0.5, 0.5));
        hand2->setPosition(head->getPosition() + vector3df(0, -16, 8));
    }

    //Stuff for foot1
    if (foot1)
    {
        foot1->setParent(head);
        foot1->setMaterialFlag(EMF_LIGHTING, true);
        foot1->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
        foot1->setScale(vector3df(0.5, 0.5, 0.5));
        foot1->setPosition(head->getPosition() + vector3df(0, -27, 6));
    }

    //Stuff for foot2
    if (foot2)
    {
        foot2->setParent(head);
        foot2->setMaterialFlag(EMF_LIGHTING, true);
        foot2->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
        foot2->setScale(vector3df(0.5, 0.5, 0.5));
        foot2->setPosition(head->getPosition() + vector3df(0, -27, -6));
    }

    //Create the camera
    ICameraSceneNode* camera = smgr->addCameraSceneNode(0, vector3df(-50, 0, 0), vector3df(head->getPosition()));
    camera->setParent(head);
    camera->setTarget(head->getPosition());
    //camera->setPosition(vector3df(-50, 0, 0));

    //Hide or show the mouse cursor
    device->getCursorControl()->setVisible(true);

    while(device->run())
    {
        //Detect mouse key presses
        if (receiver.IsKeyDown(KEY_KEY_W))
        {
            head->setPosition(head->getPosition() + vector3df(0, 0, 1));
        }
        if (receiver.IsKeyDown(KEY_KEY_S))
        {
            head->setPosition(head->getPosition() + vector3df(0, 0, -1));
        }
        if (receiver.IsKeyDown(KEY_KEY_A))
        {
            head->setRotation(head->getRotation() + vector3df(1, 0, 0));
        }
        if (receiver.IsKeyDown(KEY_KEY_D))
        {
            head->setRotation(head->getRotation() + vector3df(1, 0, 0));
        }

        camera->setTarget(head->getPosition());

        driver->beginScene(true, true, SColor(0,200,200,200));

        smgr->drawAll();
        guienv->drawAll();

        driver->endScene();
    }

    device->drop();

    return 0;
}
If it matters, I am using Code::Blocks with the MiniGW compiler. All of the examples from Irrlicht work, and I am using an EEE Netbook. I've already tested to see if the meshes could mmove at well, which they could. The "Test Cube" is just to see if the model is moving. If you need any more info, please ask. Again, I'm sorry if the answer if a simple one.
Dark_Kilauea
Posts: 368
Joined: Tue Aug 21, 2007 1:43 am
Location: The Middle of Nowhere

Post by Dark_Kilauea »

You seem to have forgotten to set the event receiver for the device. You can do this either by using device->setEventReceiver( ... ) or by the last parameter in createDevice( ... ).
rogerborg wrote:Every time someone learns to use a debugger, an angel gets their wings.
elkar
Posts: 8
Joined: Tue Jan 13, 2009 10:02 pm

Thanks

Post by elkar »

Yah, I just saw that literally 30 seconds ago. Thank you for the help anyways!
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

After you create the device, set the event receiver:

Code: Select all

device->setEventReceiver(&receiver);
Job's a good 'un.

[Edit] Damn my habit of wasting precious posting time compiling 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
Post Reply