Tutorial *37*

Editors note: this is not a complete tutorial, but taken from a forum post on this subject. If anyone feels the need to comment/describe the code some further, feel free to send in improvements to tutorials@quakesrc.org

Here is my code for managing international keyboard.
Feel free to correct, complete or comment it. it has been tested, but it is provided without guarantee. I know nothing about asiatic keyboard, maybe somebody could tell us if this code work.

in qcommon/common.c

1/ Search the code :



void Key_Init (void);

Then add :



void KeyTrans_Init (void);

2/ Search the code :



    SV_Init () ;

    CL_Init () ;

Then add :



    KeyTrans_Init() ;

in client/key.c

3/ Search the code :



int        shift_down=false;

Then add :



int        alt_down=false;

int        ctrl_down=false;

4/ Search the code :



    {"SEMICOLON", ';'},    // because a raw semicolon seperates commands



    {NULL,0}

};

Then add :



//-----------------------------------------------------------------------------

// Load a file for translation of key code for international keyboard

// If the file doesn't exist, create it with internal values

//-----------------------------------------------------------------------------

#define SIZE_KEY_TRANSTAB   ('~'-' ')

static char KeyTransTab[SIZE_KEY_TRANSTAB+1];            // key to map

static char KeyTransTab_altgr[SIZE_KEY_TRANSTAB+1];

static qboolean l_bKeyTrans = false ;

//-----------------------------------------------------------------------------

void KeyTrans_Init (void)

{

    unsigned char   *pStr;

    char            *keytrans;

    char            *pbuf;

    int             i;



    keytrans = Cvar_VariableString ("keytrans") ;

    if ( *keytrans )

    {

        FS_LoadFile(keytrans, &pbuf) ;

        if ( pbuf )

        {

            pStr = strtok(pbuf, "\n\r") ;

            if ( strlen(pStr) >= SIZE_KEY_TRANSTAB )

            {

                memcpy(KeyTransTab, pStr, SIZE_KEY_TRANSTAB) ;

                pStr = strtok(NULL, "\n\r") ;

                if ( strlen(pStr) >= SIZE_KEY_TRANSTAB )

                {

                    for ( i=0 ; i= SIZE_KEY_TRANSTAB )

                    {

                        memcpy(KeyTransTab_altgr, pStr, SIZE_KEY_TRANSTAB) ;

                        l_bKeyTrans = true ;

                    }

                }

            }

            if ( !l_bKeyTrans )

                Com_Printf("ERROR KeyTrans : Bad file %s\n", keytrans);

            Z_Free(pbuf);

        }

        else

        {

            FILE    *file;

            char    szFileName[MAX_OSPATH];



            Com_Printf("KeyTrans : Couldn't load %s, create it", keytrans);



            Com_sprintf (szFileName, sizeof(szFileName), "%s/%s", FS_Gamedir(), keytrans);



            FS_CreatePath (szFileName);

            file = fopen (szFileName, "wt");

            if ( file )

            {

                // standard

                for ( i=0 ; i

                for ( i=0 ; i

                for ( i=0 ; i ' ' && *pKey < '~' && !shift_down )

    {

        if ( KeyTransTab[*pKey-' '] != ' ' )

        {

            *pKey = KeyTransTab[*pKey-' '] ;

            if ( alt_down && ctrl_down )

            {

                if ( KeyTransTab_altgr[*pKey-' '] != ' ' )

                    *pKey = KeyTransTab_altgr[*pKey-' '] ;

            }

        }

    }

}

5/ Search the code :



void Key_Event (int key, qboolean down, unsigned time)

{

    char    *kb;

    char    cmd[1024];



    // hack for modal presses

    if (key_waiting == -1)

    {

        if (down)

            key_waiting = key;

        return;

    }

Then add :



    if ( l_bKeyTrans )

        KeyTrans(&key, down, time) ;

6/ Search the code :



    if (key == K_SHIFT)

        shift_down = down;

Then add :



    else if (key == K_ALT)

        alt_down = down;

    else if (key == K_CTRL)

        ctrl_down = down;

Compile your project and run it with '+set keytrans qwerty.txt' in the command line. The first time, it will create a file with the default state of the keys. You can copy the file in 'yourfile.txt' and modify it. The file contain 3 lines : symbol corresponding to keys, keys + and keys + . The 3 lines must have the exact lenght (complete with space like the first file generated).Then, you can run quake2 with '+set keytrans yourfile.txt'.

I have already done the file azerty.txt for french keyboard.



 
Not logged in
Sign up
Login:
Passwd: