KeyboardTranslator         Translate from english keyboard layout to any other, for instance german

This class is a simple translator from the english keyboard layout to any other. At the momment, you can only translate to german keyboard layout, because I don't know the others. You may of course add your keymaps if you want. Have fun with this class:

KeyboardTranslator.h

#ifndef KEYBTRANS_H
#define KEYBTRANS_H

class KeyboardTranslator : public Singleton<KeyboardTranslator>
{
public:
    enum KeyMap
    {
        KEYMAP_GERMAN  = 1, 
        KEYMAP_ENGLISH = 2,
        KEYMAP_FRENCH  = 3
    };

    static KeyboardTranslator* getSingletonPtr(void);
    static KeyboardTranslator& getSingleton(void);

    KeyboardTranslator(KeyMap keymap = KEYMAP_GERMAN);
    virtual ~KeyboardTranslator();

    void translate(unsigned char key);
    unsigned char getKey();

private:
    KeyMap mKeymap;
    unsigned char mKey;
    unsigned char mTranslatedKey;
};

#endif


KeyboardTranslator.cpp

#include "KeyboardTranslator.h"

template<> KeyboardTranslator* Singleton<KeyboardTranslator>::ms_Singleton = 0;

/* -------------------------------------------------------- */
/// Singleton function
/* -------------------------------------------------------- */
KeyboardTranslator* KeyboardTranslator::getSingletonPtr(void)
{
    return ms_Singleton;
}

/* -------------------------------------------------------- */
/// Singleton function
/* -------------------------------------------------------- */
KeyboardTranslator& KeyboardTranslator::getSingleton(void)
{
    assert(ms_Singleton); 
    return (*ms_Singleton);
}

/* -------------------------------------------------------- */
/// Constructor
/* -------------------------------------------------------- */
KeyboardTranslator::KeyboardTranslator(KeyMap keymap)
{
    // Set keymap (default is german)
    mKeymap = keymap;
}

/* -------------------------------------------------------- */
/// Destructor
/* -------------------------------------------------------- */
KeyboardTranslator::~KeyboardTranslator()
{}

/* -------------------------------------------------------- */
/// Translation function
/* -------------------------------------------------------- */
void KeyboardTranslator::translate(unsigned char key)
{
    mKey = key;

    /* ------------------------------------------------ */
    // English Keymap
    /* ------------------------------------------------ */
    if (mKeymap == KEYMAP_ENGLISH)
    {
        mTranslatedKey = mKey;
        return;
    }

    /* ------------------------------------------------ */
    // German Keymap
    /* ------------------------------------------------ */
    else if (mKeymap == KEYMAP_GERMAN)
    {
        const unsigned char KEYS_TO_CHANGE = 28;
        unsigned char array_german[KEYS_TO_CHANGE] = {'y', 'z', 246, 228, 252, ':', ';', '?', '_', ')', '=', 180, '(', '\'', '#', '/', '&', 167, '"', '^', 176, '+', 214, 196, '-', 220, '*', 223 };
        unsigned char array_english[KEYS_TO_CHANGE] = {'z', 'y', ';', '\'', '[', '>', '<', '_', '?', '(', ')', '+', '*', '|', '\\', '&', '^', '#', '@', '`', '~', ']', ':', ';', '/', '{', '}', '-'};
                
        mTranslatedKey = mKey;
        for (unsigned char charIterator = 0; charIterator < KEYS_TO_CHANGE; charIterator++)
        {
            if (mKey == array_english[charIterator])
            {
                mTranslatedKey = array_german[charIterator];
                return;
            }
        }
        return;
    }

    /* ------------------------------------------------ */
    // French Keymap (95% complete)
    /* ------------------------------------------------ */
    else if (mKeymap == KEYMAP_FRENCH)
    {
        const unsigned char KEYS_TO_CHANGE = 44;
        unsigned char array_french[KEYS_TO_CHANGE] = 
{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '°', 'a', 'A', 'z', 'Z', '£', '$', '&', 'é', '\"', '\'', '(', '-', 'è', '\\', 'ç', 'à', 'q', 'Q', 'M', 'm', '%', 'ù', 'w', 'W', '?', ',', '.', ';', '/', ':', '§', '!' };
        unsigned char array_english[KEYS_TO_CHANGE] = 
{'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', 'q', 'Q', 'w', 'W', '}', ']', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'A', '?', ',', '\"', '\'', 'z', 'Z', 'M', 'm', '<', ',', '>', '.', '?', '/' };
                
        mTranslatedKey = mKey;
        for (unsigned char charIterator = 0; charIterator < KEYS_TO_CHANGE; charIterator++)
        {
            if (mKey == array_english[charIterator])
            {
                mTranslatedKey = array_french[charIterator];
                return;
            }
        }
        return;
    }

    /* ------------------------------------------------ */
    // Italian Keymap (by Mortymer for OWG::Legion project)
    /* ------------------------------------------------ */
    else if (mKeymap == KEYMAP_ITALIAN)
    {
        const unsigned char KEYS_TO_CHANGE = 27;
        unsigned char array_english[KEYS_TO_CHANGE] = 
{'`', '~', '@', '#', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', '{', ']', '}', '\\','|', ';', ':', '\'','"', '<', '>', '/', '?' };
        unsigned char array_italian[KEYS_TO_CHANGE] = 
{'\\','|', '"', '£', '&', '/', '(', ')', '=', '\'','?', 'ì', '^', 'è', 'é', '+', '*', 'ù', '§', 'ò', 'ç', 'à', '°', ';', ':', '-', '_'};
                
                //// excluded from ita: [ (null) ] (null) @ (null) # (null) < (\ replica) > (| replica) 
        mTranslatedKey = mKey;
        for (unsigned char charIterator = 0; charIterator < KEYS_TO_CHANGE; charIterator++)
        {
            if (mKey == array_english[charIterator])
            {
                mTranslatedKey = array_italian[charIterator];
                return;
            }
        }
        return;
    }

        /// Here you can add your own keymaps, just copy from the if(){} above
}

/* -------------------------------------------------------- */
/// 
/* -------------------------------------------------------- */
unsigned char KeyboardTranslator::getKey()
{
    return mTranslatedKey;
}


And at last here a little example how to use the translator:

//Somewhere in your initialisation progress do this:
new KeyboardTranslator(KeyboardTranslator::KEYMAP_GERMAN);

//later in your keyPressed/keyReleased methods you can do this:
void YourListenerClass::keyPressed(KeyEvent *e)
{
    (...)
    KeyboardTranslator::getSingleton().translate(e->getKeyChar());
    unsigned char translatedKey = KeyboardTranslator::getSingleton().getKey();
    (...)

    //if your using CEGUI, you can inject the keys:
    CEGUI::System::getSingleton().injectKeyDown(e->getKey());
    CEGUI::System::getSingleton().injectChar(translatedKey);
}


Another useful information:
If you want to display special letters, for example ä, in CEGUI, you have to define the according glyphs in the fonts. The following example makes it possible to show ä, ö, ü and ß within the CEGUI Windows using font "Tahoma-12":

CEGUI::Font* f;
f = FontManager::getSingleton().createFont("Tahoma-12", "Tahoma.ttf", 12, 0);
f->defineFontGlyphs(f->getAvailableGlyphs() + (utf32)223 + (utf32)228 + (utf32)246 + (utf32)252);