Change Key Mapping

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
jcarew
Posts: 34
Joined: Wed Jul 04, 2007 3:54 pm

Change Key Mapping

Post by jcarew »

How can I change mapping of my key (for Controler Setup in my project). Eg. 'W' to 'K' etc.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I'm assuming that you are talking about the camera mapping for manipulating the fps camera. If that is what you are talking about, then there is a keyMap parameter that can be passed to the addCameraSceneNodeFPS() call that creates the camera.

If you are talking about something else, then maybe you should provide us with more details. Mind reading is more difficult than it looks...

Travis
konrad
Posts: 15
Joined: Wed Oct 03, 2007 7:07 pm
Location: Poland

Post by konrad »

Use keyMaps ;p

Code: Select all

 SKeyMap keyMap[5];
 keyMap[0].Action=EKA_MOVE_FORWARD;   keyMap[0].KeyCode=KEY_KEY_W;
 keyMap[1].Action=EKA_MOVE_BACKWARD;  keyMap[1].KeyCode=KEY_KEY_S;
 keyMap[2].Action=EKA_STRAFE_LEFT;    keyMap[2].KeyCode=KEY_KEY_A;
 keyMap[3].Action=EKA_STRAFE_RIGHT;   keyMap[3].KeyCode=KEY_KEY_D;
 keyMap[4].Action=EKA_JUMP_UP;        keyMap[4].KeyCode=KEY_SPACE;
scene::ICameraSceneNode* Cam=smgr->addCameraSceneNodeFPS(0,100,500,-1,keyMap,5,false,0);
PS: Thanks for education Travis :)
jcarew
Posts: 34
Joined: Wed Jul 04, 2007 3:54 pm

Post by jcarew »

I can't use KeyMap because I don't use FPS Camera
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Then what do you use?

In general, what you'll want is a configurable mapping of inputs to game events. What constitutes an 'input' and a 'game event' is dependant on your project.

If you're using Irrlicht events, then a (very) simple way of doing keymapping is provided in the following code. It maps Irrlicht keys to other Irrlicht keys. In general though, you should define your own game events, and map keys / mouse events / joystick events to game events.

How you allow your users to actually configure the mapping is up to you; you'll have to write your own GUI. To make them persistant, you could (trivially) just fwrite/fread all your mappings array directly to/from a (binary mode) file, or you could waste your time arsing around with some overblown XML schema.

You can use this implementation of IEventReceiver to replace the one in example 04, Movement, to see it working. Normally you'd press W and S to move the node up and down; here, Q has been mapped to W (but S isn't remapped) so the keys are Q and S. Note that OnTranslatedEvent() is just the existing OnEvent(), expecting the same 'hard coded' key events.

Code: Select all

class ConfigurableEventReceiver : public IEventReceiver
{
public:
    // The map of translations, one for each key.  Public so
    // that your app can set these mappings itself.
    EKEY_CODE KeyTranslations[KEY_KEY_CODES_COUNT];
    
    MyEventReceiver()
    {
        for(int key = 0; key < KEY_KEY_CODES_COUNT; ++key)
            KeyTranslations[key] = (EKEY_CODE)key; // By default, map each key to itself

        // The next bits should really be done by your application.
        // Map Q to W
        KeyTranslations[KEY_KEY_Q] = KEY_KEY_W;

        // Remove the default mappings for W by setting an invalid key code
        KeyTranslations[KEY_KEY_W] = KEY_KEY_CODES_COUNT;
    }

    virtual bool OnEvent(const SEvent& event)
    {
        SEvent translatedEvent(event);

        if (event.EventType == irr::EET_KEY_INPUT_EVENT)
            translatedEvent.KeyInput.Key = KeyTranslations[event.KeyInput.Key];

        return OnTranslatedEvent(translatedEvent);
    }

private:

    virtual bool OnTranslatedEvent(const SEvent & event)
    {
        if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT&&
			!event.KeyInput.PressedDown)
        {
            switch(event.KeyInput.Key)
            {
                case KEY_KEY_W: // You actually need to press Q to get a W key now
                case KEY_KEY_S: // But S uses its standard mapping
                {
                    core::vector3df v = node->getPosition();
                    v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
                    node->setPosition();
                }
	    
                return true;
            }
        }

        return false;
    }
};
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
newbie8787
Posts: 105
Joined: Thu Jan 10, 2008 6:26 pm

is this a copy constructor , or something to the effect?

Post by newbie8787 »

i was wondering what the line SEvent translatedEvent(event); does in the code ? from what i can gather it is something like a copy constructor for Sevent , although Sevent is a structure, please do correct me if i am wrong (which i think i am coz structures cant have constructors i think , also i saw the Sevent code and there is no such thing!)

and many thanks to rogerborg for this, i desperateley needed it! :D
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Structs certainly can have constructors and functions.

translatedEvent is just a copy of the event passed in and then edited, because the event passed in is const so can't be changed.
Image Image Image
newbie8787
Posts: 105
Joined: Thu Jan 10, 2008 6:26 pm

thanks JP

Post by newbie8787 »

thanks ... i guess i was right then :D
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Yup. It's equivalent to SEvent translatedEvent = event;

I habitually use the explicit copy-constructor to be absolutely sure that the event isn't created with the default constructor first and then assigned (which would be slightly inefficient). However vitek recently convinced me that all modern compilers bypass that, and use the copy-constructor when = is used on the declaration.
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