
#include <stdio.h>
#include <string.h>
#include "types.h"
#include "debug.h"
#include "demo.h"
#include "task.h"
#include "key.h"
#include "options.h"
#include "consmgr.h"

/*=============================================================================
    Data:
=============================================================================*/
volatile keyScanType keyScanCode[KEY_TOTAL_KEYS];
volatile keyScanType keySaveScan[KEY_TOTAL_KEYS];
real32 keyLastTimeHit = 0.0f;

udword keyTableGermanToEnglish[KEY_TOTAL_KEYS]=
{
    /*            0 0  */  0,
    /*              1  */  1,
    /*              2  */  2,
    /*              3  */  3,
    /*              4  */  4,
    /*              5  */  5,
    /*              6  */  6,
    /*              7  */  7,
    /*              8  */  8,
    /*              9  */  9,
    /*              10 */  10,
    /*              11 */  11,
    /*              12 */  12,
    /*              13 */  13,
    /*              14 */  14,
    /*              15 */  15,
    /*           16 16 */  16,
    /*              17 */  17,
    /*              18 */  18,
    /*              19 */  19,
    /*              20 */  20,
    /*              21 */  21,
    /*              22 */  22,
    /*              23 */  23,
    /*              24 */  24,
    /*              25 */  25,
    /*              26 */  26,
    /*              27 */  27,
    /*              28 */  28,
    /*              29 */  29,
    /*              30 */  30,
    /*              31 */  31,
    /* space     32 32 */  32,
    /*              33 */  33,
    /*              34 */  34,
    /*              35 */  35,
    /*              36 */  36,
    /*              37 */  37,
    /*              38 */  38,
    /* punctuation  39 */  39,
    /*              40 */  40,
    /*              41 */  41,
    /*              42 */  42,
    /*              43 */  43,
    /*              44 */  44,
    /*              45 */  45,
    /*              46 */  46,
    /*              47 */  47,
    /* 0         48 48 */  48,
    /* 1            49 */  49,
    /* 2            50 */  50,
    /* 3            51 */  51,
    /* 4            52 */  52,
    /* 5            53 */  53,
    /* 6            54 */  54,
    /* 7            55 */  55,
    /* 8            56 */  56,
    /* 9            57 */  57,
    /* :            58 */  58,
    /* ;            59 */  59,
    /* <            60 */  60,
    /* =            61 */  61,
    /* >            62 */  62,
    /* ?            63 */  63,
    /* @         64 64 */  64,
    /* A            65 */  65,
    /* B            66 */  66,
    /* C            67 */  67,
    /* D            68 */  68,
    /* E            69 */  69,
    /* F            70 */  70,
    /* G            71 */  71,
    /* H            72 */  72,
    /* I            73 */  73,
    /* J            74 */  74,
    /* K            75 */  75,
    /* L            76 */  76,
    /* M            77 */  77,
    /* N            78 */  78,
    /* O            79 */  79,
    /* P         80 80 */  80,
    /* Q            81 */  81,
    /* R            82 */  82,
    /* S            83 */  83,
    /* T            84 */  84,
    /* U            85 */  85,
    /* V            86 */  86,
    /* W            87 */  87,
    /* X            88 */  88,
    /* Y            89 */  89,
    /* Z            90 */  90,
    /*              91 */  91,
    /*              92 */  92,
    /*              93 */  93,
    /*              94 */  94,
    /*              95 */  95,
    /*           96 96 */  96,
    /*              97 */  97,
    /*              98 */  98,
    /*              99 */  99,
    /*              100*/  100,
    /*              101*/  101,
    /*              102*/  102,
    /*              103*/  103,
    /*              104*/  104,
    /*              105*/  105,
    /*              106*/  106,
    /*              107*/  107,
    /*              108*/  108,
    /*              109*/  109,
    /*              110*/  110,
    /*              111*/  111,
    /*          112 112*/  112,
    /*              113*/  113,
    /*              114*/  114,
    /*              115*/  115,
    /*              116*/  116,
    /*              117*/  117,
    /*              118*/  118,
    /*              119*/  119,
    /*              120*/  120,
    /*              121*/  121,
    /*              122*/  122,
    /*              123*/  123,
    /*              124*/  124,
    /*              125*/  125,
    /*              126*/  126,
    /*              127*/  127,
    /*          128 128*/  128,
    /*              129*/  129,
    /*              130*/  130,
    /*              131*/  131,
    /*              132*/  132,
    /*              133*/  133,
    /*              134*/  134,
    /*              135*/  135,
    /*              136*/  136,
    /*              137*/  137,
    /*              138*/  138,
    /*              139*/  139,
    /*              140*/  140,
    /*              141*/  141,
    /*              142*/  142,
    /*              143*/  143,
    /*          144 144*/  144,
    /*              145*/  145,
    /*              146*/  146,
    /*              147*/  147,
    /*              148*/  148,
    /*              149*/  149,
    /*              150*/  150,
    /*              151*/  151,
    /*              152*/  152,
    /*              153*/  153,
    /*              154*/  154,
    /*              155*/  155,
    /*              156*/  156,
    /*              157*/  157,
    /*              158*/  158,
    /*              159*/  159,
    /*          160 160*/  160,
    /*              161*/  161,
    /*              162*/  162,
    /*              163*/  163,
    /*              164*/  164,
    /*              165*/  165,
    /*              166*/  166,
    /*              167*/  167,
    /*              168*/  168,
    /*              169*/  169,
    /*              170*/  170,
    /*              171*/  171,
    /*              172*/  172,
    /*              173*/  173,
    /*              174*/  174,
    /*              175*/  175,
    /*          176 176*/  176,
    /*              177*/  177,
    /*              178*/  178,
    /*              179*/  179,
    /*              180*/  180,
    /*              181*/  181,
    /*              182*/  182,
    /*              183*/  183,
    /*              184*/  184,
    /*              185*/  185,
    /*              186*/  LBRACK,
    /*              187*/  RBRACK,
    /*              188*/  LESSTHAN,
    /*              189*/  200,
    /*              190*/  GREATERTHAN,
    /*              191*/  BACKSLASHKEY,
    /*          192 192*/  246, // '' character
    /*              193*/  193,
    /*              194*/  194,
    /*              195*/  195,
    /*              196*/  196,
    /*              197*/  197,
    /*              198*/  198,
    /*              199*/  199,
    /*              200*/  200,
    /*              201*/  201,
    /*              202*/  202,
    /*              203*/  203,
    /*              204*/  204,
    /*              205*/  205,
    /*              206*/  206,
    /*              207*/  207,
    /*          208 208*/  208,
    /*              209*/  209,
    /*              210*/  210,
    /*              211*/  211,
    /*              212*/  212,
    /*              213*/  213,
    /*              214*/  214,
    /*              215*/  215,
    /*              216*/  216,
    /*              217*/  217,
    /*              218*/  218,
    /*              219*/  MINUSKEY,
    /*              220*/  TILDEKEY,
    /*              221*/  PLUSKEY,
    /*              222*/  228, // '' character
    /*              223*/  223,
    /*          224 224*/  224,
    /*              225*/  225,
    /*              226*/  226,
    /*              227*/  227,
    /*              228*/  228,
    /*              229*/  229,
    /*              230*/  230,
    /*              231*/  231,
    /*              232*/  232,
    /*              233*/  233,
    /*              234*/  234,
    /*              235*/  235,
    /*              236*/  236,
    /*              237*/  237,
    /*              238*/  238,
    /*              239*/  239,
    /*          240 240*/  240,
    /*              241*/  241,
    /*              242*/  242,
    /*              243*/  243,
    /*              244*/  244,
    /*              245*/  245,
    /*              246*/  246,
    /*              247*/  247,
    /*              248*/  248,
    /*              249*/  249,
    /*              250*/  250,
    /*              251*/  251,
    /*              252*/  252,
    /*              253*/  253,
    /*              254*/  254,
    /*              255*/  255,
};

udword keyTableFrenchToEnglish[KEY_TOTAL_KEYS] =
{
    /*            0 0  */  0,
    /*              1  */  1,
    /*              2  */  2,
    /*              3  */  3,
    /*              4  */  4,
    /*              5  */  5,
    /*              6  */  6,
    /*              7  */  7,
    /*              8  */  8,
    /*              9  */  9,
    /*              10 */  10,
    /*              11 */  11,
    /*              12 */  12,
    /*              13 */  13,
    /*              14 */  14,
    /*              15 */  15,
    /*           16 16 */  16,
    /*              17 */  17,
    /*              18 */  18,
    /*              19 */  19,
    /*              20 */  20,
    /*              21 */  21,
    /*              22 */  22,
    /*              23 */  23,
    /*              24 */  24,
    /*              25 */  25,
    /*              26 */  26,
    /*              27 */  27,
    /*              28 */  28,
    /*              29 */  29,
    /*              30 */  30,
    /*              31 */  31,
    /* space     32 32 */  32,
    /*              33 */  33,
    /*              34 */  34,
    /*              35 */  35,
    /*              36 */  36,
    /*              37 */  37,
    /*              38 */  38,
    /* punctuation  39 */  39,
    /*              40 */  40,
    /*              41 */  41,
    /*              42 */  42,
    /*              43 */  43,
    /*              44 */  44,
    /*              45 */  45,
    /*              46 */  46,
    /*              47 */  47,
    /* 0         48 48 */  48,
    /* 1            49 */  49,
    /* 2            50 */  50,
    /* 3            51 */  51,
    /* 4            52 */  52,
    /* 5            53 */  53,
    /* 6            54 */  54,
    /* 7            55 */  55,
    /* 8            56 */  56,
    /* 9            57 */  57,
    /* :            58 */  58,
    /* ;            59 */  59,
    /* <            60 */  60,
    /* =            61 */  61,
    /* >            62 */  62,
    /* ?            63 */  63,
    /* @         64 64 */  64,
    /* A            65 */  65,
    /* B            66 */  66,
    /* C            67 */  67,
    /* D            68 */  68,
    /* E            69 */  69,
    /* F            70 */  70,
    /* G            71 */  71,
    /* H            72 */  72,
    /* I            73 */  73,
    /* J            74 */  74,
    /* K            75 */  75,
    /* L            76 */  76,
    /* M            77 */  77,
    /* N            78 */  78,
    /* O            79 */  79,
    /* P         80 80 */  80,
    /* Q            81 */  81,
    /* R            82 */  82,
    /* S            83 */  83,
    /* T            84 */  84,
    /* U            85 */  85,
    /* V            86 */  86,
    /* W            87 */  87,
    /* X            88 */  88,
    /* Y            89 */  89,
    /* Z            90 */  90,
    /*              91 */  91,
    /*              92 */  92,
    /*              93 */  93,
    /*              94 */  94,
    /*              95 */  95,
    /*           96 96 */  96,
    /*              97 */  97,
    /*              98 */  98,
    /*              99 */  99,
    /*              100*/  100,
    /*              101*/  101,
    /*              102*/  102,
    /*              103*/  103,
    /*              104*/  104,
    /*              105*/  105,
    /*              106*/  106,
    /*              107*/  107,
    /*              108*/  108,
    /*              109*/  109,
    /*              110*/  110,
    /*              111*/  111,
    /*          112 112*/  112,
    /*              113*/  113,
    /*              114*/  114,
    /*              115*/  115,
    /*              116*/  116,
    /*              117*/  117,
    /*              118*/  118,
    /*              119*/  119,
    /*              120*/  120,
    /*              121*/  121,
    /*              122*/  122,
    /*              123*/  123,
    /*              124*/  124,
    /*              125*/  125,
    /*              126*/  126,
    /*              127*/  127,
    /*          128 128*/  128,
    /*              129*/  129,
    /*              130*/  130,
    /*              131*/  131,
    /*              132*/  132,
    /*              133*/  133,
    /*              134*/  134,
    /*              135*/  135,
    /*              136*/  136,
    /*              137*/  137,
    /*              138*/  138,
    /*              139*/  139,
    /*              140*/  140,
    /*              141*/  141,
    /*              142*/  142,
    /*              143*/  143,
    /*          144 144*/  144,
    /*              145*/  145,
    /*              146*/  146,
    /*              147*/  147,
    /*              148*/  148,
    /*              149*/  149,
    /*              150*/  150,
    /*              151*/  151,
    /*              152*/  152,
    /*              153*/  153,
    /*              154*/  154,
    /*              155*/  155,
    /*              156*/  156,
    /*              157*/  157,
    /*              158*/  158,
    /*              159*/  159,
    /*          160 160*/  160,
    /*              161*/  161,
    /*              162*/  162,
    /*              163*/  163,
    /*              164*/  164,
    /*              165*/  165,
    /*              166*/  166,
    /*              167*/  167,
    /*              168*/  168,
    /*              169*/  169,
    /*              170*/  170,
    /*              171*/  171,
    /*              172*/  172,
    /*              173*/  173,
    /*              174*/  174,
    /*              175*/  175,
    /*          176 176*/  176,
    /*              177*/  177,
    /*              178*/  178,
    /*              179*/  179,
    /*              180*/  180,
    /*              181*/  181,
    /*              182*/  182,
    /*              183*/  183,
    /*              184*/  184,
    /*              185*/  185,
    /*              186*/  RBRACK,
    /*              187*/  187,
    /*              188*/  44,  // ','
    /*              189*/  189,
    /*              190*/  LESSTHAN,
    /*              191*/  GREATERTHAN,
    /*          192 192*/  249, // '' character
    /*              193*/  193,
    /*              194*/  194,
    /*              195*/  195,
    /*              196*/  196,
    /*              197*/  197,
    /*              198*/  198,
    /*              199*/  199,
    /*              200*/  200,
    /*              201*/  201,
    /*              202*/  202,
    /*              203*/  203,
    /*              204*/  204,
    /*              205*/  205,
    /*              206*/  206,
    /*              207*/  207,
    /*          208 208*/  208,
    /*              209*/  209,
    /*              210*/  210,
    /*              211*/  211,
    /*              212*/  212,
    /*              213*/  213,
    /*              214*/  214,
    /*              215*/  215,
    /*              216*/  216,
    /*              217*/  217,
    /*              218*/  218,
    /*              219*/  MINUSKEY,
    /*              220*/  BACKSLASHKEY,
    /*              221*/  LBRACK,
    /*              222*/  TILDEKEY,
    /*              223*/  167,  // ''  character
    /*          224 224*/  224,
    /*              225*/  225,
    /*              226*/  226,
    /*              227*/  227,
    /*              228*/  228,
    /*              229*/  229,
    /*              230*/  230,
    /*              231*/  231,
    /*              232*/  232,
    /*              233*/  233,
    /*              234*/  234,
    /*              235*/  235,
    /*              236*/  236,
    /*              237*/  237,
    /*              238*/  238,
    /*              239*/  239,
    /*          240 240*/  240,
    /*              241*/  241,
    /*              242*/  242,
    /*              243*/  243,
    /*              244*/  244,
    /*              245*/  245,
    /*              246*/  246,
    /*              247*/  247,
    /*              248*/  248,
    /*              249*/  249,
    /*              250*/  250,
    /*              251*/  251,
    /*              252*/  252,
    /*              253*/  253,
    /*              254*/  254,
    /*              255*/  255,
};

udword keyTableSpanishToEnglish[KEY_TOTAL_KEYS]=
{
    /*            0 0  */  0,
    /*              1  */  1,
    /*              2  */  2,
    /*              3  */  3,
    /*              4  */  4,
    /*              5  */  5,
    /*              6  */  6,
    /*              7  */  7,
    /*              8  */  8,
    /*              9  */  9,
    /*              10 */  10,
    /*              11 */  11,
    /*              12 */  12,
    /*              13 */  13,
    /*              14 */  14,
    /*              15 */  15,
    /*           16 16 */  16,
    /*              17 */  17,
    /*              18 */  18,
    /*              19 */  19,
    /*              20 */  20,
    /*              21 */  21,
    /*              22 */  22,
    /*              23 */  23,
    /*              24 */  24,
    /*              25 */  25,
    /*              26 */  26,
    /*              27 */  27,
    /*              28 */  28,
    /*              29 */  29,
    /*              30 */  30,
    /*              31 */  31,
    /* space     32 32 */  32,
    /*              33 */  33,
    /*              34 */  34,
    /*              35 */  35,
    /*              36 */  36,
    /*              37 */  37,
    /*              38 */  38,
    /* punctuation  39 */  39,
    /*              40 */  40,
    /*              41 */  41,
    /*              42 */  42,
    /*              43 */  43,
    /*              44 */  44,
    /*              45 */  45,
    /*              46 */  46,
    /*              47 */  47,
    /* 0         48 48 */  48,
    /* 1            49 */  49,
    /* 2            50 */  50,
    /* 3            51 */  51,
    /* 4            52 */  52,
    /* 5            53 */  53,
    /* 6            54 */  54,
    /* 7            55 */  55,
    /* 8            56 */  56,
    /* 9            57 */  57,
    /* :            58 */  58,
    /* ;            59 */  59,
    /* <            60 */  60,
    /* =            61 */  61,
    /* >            62 */  62,
    /* ?            63 */  63,
    /* @         64 64 */  64,
    /* A            65 */  65,
    /* B            66 */  66,
    /* C            67 */  67,
    /* D            68 */  68,
    /* E            69 */  69,
    /* F            70 */  70,
    /* G            71 */  71,
    /* H            72 */  72,
    /* I            73 */  73,
    /* J            74 */  74,
    /* K            75 */  75,
    /* L            76 */  76,
    /* M            77 */  77,
    /* N            78 */  78,
    /* O            79 */  79,
    /* P         80 80 */  80,
    /* Q            81 */  81,
    /* R            82 */  82,
    /* S            83 */  83,
    /* T            84 */  84,
    /* U            85 */  85,
    /* V            86 */  86,
    /* W            87 */  87,
    /* X            88 */  88,
    /* Y            89 */  89,
    /* Z            90 */  90,
    /*              91 */  91,
    /*              92 */  92,
    /*              93 */  93,
    /*              94 */  94,
    /*              95 */  95,
    /*           96 96 */  96,
    /*              97 */  97,
    /*              98 */  98,
    /*              99 */  99,
    /*              100*/  100,
    /*              101*/  101,
    /*              102*/  102,
    /*              103*/  103,
    /*              104*/  104,
    /*              105*/  105,
    /*              106*/  106,
    /*              107*/  107,
    /*              108*/  108,
    /*              109*/  109,
    /*              110*/  110,
    /*              111*/  111,
    /*          112 112*/  112,
    /*              113*/  113,
    /*              114*/  114,
    /*              115*/  115,
    /*              116*/  116,
    /*              117*/  117,
    /*              118*/  118,
    /*              119*/  119,
    /*              120*/  120,
    /*              121*/  121,
    /*              122*/  122,
    /*              123*/  123,
    /*              124*/  124,
    /*              125*/  125,
    /*              126*/  126,
    /*              127*/  127,
    /*          128 128*/  128,
    /*              129*/  129,
    /*              130*/  130,
    /*              131*/  131,
    /*              132*/  132,
    /*              133*/  133,
    /*              134*/  134,
    /*              135*/  135,
    /*              136*/  136,
    /*              137*/  137,
    /*              138*/  138,
    /*              139*/  139,
    /*              140*/  140,
    /*              141*/  141,
    /*              142*/  142,
    /*              143*/  143,
    /*          144 144*/  144,
    /*              145*/  145,
    /*              146*/  146,
    /*              147*/  147,
    /*              148*/  148,
    /*              149*/  149,
    /*              150*/  150,
    /*              151*/  151,
    /*              152*/  152,
    /*              153*/  153,
    /*              154*/  154,
    /*              155*/  155,
    /*              156*/  156,
    /*              157*/  157,
    /*              158*/  158,
    /*              159*/  159,
    /*          160 160*/  160,
    /*              161*/  161,
    /*              162*/  162,
    /*              163*/  163,
    /*              164*/  164,
    /*              165*/  165,
    /*              166*/  166,
    /*              167*/  167,
    /*              168*/  168,
    /*              169*/  169,
    /*              170*/  170,
    /*              171*/  171,
    /*              172*/  172,
    /*              173*/  173,
    /*              174*/  174,
    /*              175*/  175,
    /*          176 176*/  176,
    /*              177*/  177,
    /*              178*/  178,
    /*              179*/  179,
    /*              180*/  180,
    /*              181*/  181,
    /*              182*/  182,
    /*              183*/  183,
    /*              184*/  184,
    /*              185*/  185,
    /*              186*/  LBRACK,  // 219
    /*              187*/  RBRACK,  // 221
    /*              188*/  LESSTHAN,// 188
    /*              189*/  200,     // just for ease
    /*              190*/  GREATERTHAN, // 190
    /*              191*/  BACKSLASHKEY,  // 220
    /*          192 192*/  241,  // '' character
    /*              193*/  193,
    /*              194*/  194,
    /*              195*/  195,
    /*              196*/  196,
    /*              197*/  197,
    /*              198*/  198,
    /*              199*/  199,
    /*              200*/  200,
    /*              201*/  201,
    /*              202*/  202,
    /*              203*/  203,
    /*              204*/  204,
    /*              205*/  205,
    /*              206*/  206,
    /*              207*/  207,
    /*          208 208*/  208,
    /*              209*/  209,
    /*              210*/  210,
    /*              211*/  211,
    /*              212*/  212,
    /*              213*/  213,
    /*              214*/  214,
    /*              215*/  215,
    /*              216*/  216,
    /*              217*/  217,
    /*              218*/  218,
    /*              219*/  MINUSKEY,  // 189
    /*              220*/  TILDEKEY,  // 192
    /*              221*/  PLUSKEY,   // 187
    /*              222*/  180,       // '' character
    /*              223*/  223,
    /*          224 224*/  224,
    /*              225*/  225,
    /*              226*/  226,
    /*              227*/  227,
    /*              228*/  228,
    /*              229*/  229,
    /*              230*/  230,
    /*              231*/  231,
    /*              232*/  232,
    /*              233*/  233,
    /*              234*/  234,
    /*              235*/  235,
    /*              236*/  236,
    /*              237*/  237,
    /*              238*/  238,
    /*              239*/  239,
    /*          240 240*/  240,
    /*              241*/  241,
    /*              242*/  242,
    /*              243*/  243,
    /*              244*/  244,
    /*              245*/  245,
    /*              246*/  246,
    /*              247*/  247,
    /*              248*/  248,
    /*              249*/  249,
    /*              250*/  250,
    /*              251*/  251,
    /*              252*/  252,
    /*              253*/  253,
    /*              254*/  254,
    /*              255*/  255,
};

udword keyTableItalianToEnglish[KEY_TOTAL_KEYS]=
{
    /*            0 0  */  0,
    /*              1  */  1,
    /*              2  */  2,
    /*              3  */  3,
    /*              4  */  4,
    /*              5  */  5,
    /*              6  */  6,
    /*              7  */  7,
    /*              8  */  8,
    /*              9  */  9,
    /*              10 */  10,
    /*              11 */  11,
    /*              12 */  12,
    /*              13 */  13,
    /*              14 */  14,
    /*              15 */  15,
    /*           16 16 */  16,
    /*              17 */  17,
    /*              18 */  18,
    /*              19 */  19,
    /*              20 */  20,
    /*              21 */  21,
    /*              22 */  22,
    /*              23 */  23,
    /*              24 */  24,
    /*              25 */  25,
    /*              26 */  26,
    /*              27 */  27,
    /*              28 */  28,
    /*              29 */  29,
    /*              30 */  30,
    /*              31 */  31,
    /* space     32 32 */  32,
    /*              33 */  33,
    /*              34 */  34,
    /*              35 */  35,
    /*              36 */  36,
    /*              37 */  37,
    /*              38 */  38,
    /* punctuation  39 */  39,
    /*              40 */  40,
    /*              41 */  41,
    /*              42 */  42,
    /*              43 */  43,
    /*              44 */  44,
    /*              45 */  45,
    /*              46 */  46,
    /*              47 */  47,
    /* 0         48 48 */  48,
    /* 1            49 */  49,
    /* 2            50 */  50,
    /* 3            51 */  51,
    /* 4            52 */  52,
    /* 5            53 */  53,
    /* 6            54 */  54,
    /* 7            55 */  55,
    /* 8            56 */  56,
    /* 9            57 */  57,
    /* :            58 */  58,
    /* ;            59 */  59,
    /* <            60 */  60,
    /* =            61 */  61,
    /* >            62 */  62,
    /* ?            63 */  63,
    /* @         64 64 */  64,
    /* A            65 */  65,
    /* B            66 */  66,
    /* C            67 */  67,
    /* D            68 */  68,
    /* E            69 */  69,
    /* F            70 */  70,
    /* G            71 */  71,
    /* H            72 */  72,
    /* I            73 */  73,
    /* J            74 */  74,
    /* K            75 */  75,
    /* L            76 */  76,
    /* M            77 */  77,
    /* N            78 */  78,
    /* O            79 */  79,
    /* P         80 80 */  80,
    /* Q            81 */  81,
    /* R            82 */  82,
    /* S            83 */  83,
    /* T            84 */  84,
    /* U            85 */  85,
    /* V            86 */  86,
    /* W            87 */  87,
    /* X            88 */  88,
    /* Y            89 */  89,
    /* Z            90 */  90,
    /*              91 */  91,
    /*              92 */  92,
    /*              93 */  93,
    /*              94 */  94,
    /*              95 */  95,
    /*           96 96 */  96,
    /*              97 */  97,
    /*              98 */  98,
    /*              99 */  99,
    /*              100*/  100,
    /*              101*/  101,
    /*              102*/  102,
    /*              103*/  103,
    /*              104*/  104,
    /*              105*/  105,
    /*              106*/  106,
    /*              107*/  107,
    /*              108*/  108,
    /*              109*/  109,
    /*              110*/  110,
    /*              111*/  111,
    /*          112 112*/  112,
    /*              113*/  113,
    /*              114*/  114,
    /*              115*/  115,
    /*              116*/  116,
    /*              117*/  117,
    /*              118*/  118,
    /*              119*/  119,
    /*              120*/  120,
    /*              121*/  121,
    /*              122*/  122,
    /*              123*/  123,
    /*              124*/  124,
    /*              125*/  125,
    /*              126*/  126,
    /*              127*/  127,
    /*          128 128*/  128,
    /*              129*/  129,
    /*              130*/  130,
    /*              131*/  131,
    /*              132*/  132,
    /*              133*/  133,
    /*              134*/  134,
    /*              135*/  135,
    /*              136*/  136,
    /*              137*/  137,
    /*              138*/  138,
    /*              139*/  139,
    /*              140*/  140,
    /*              141*/  141,
    /*              142*/  142,
    /*              143*/  143,
    /*          144 144*/  144,
    /*              145*/  145,
    /*              146*/  146,
    /*              147*/  147,
    /*              148*/  148,
    /*              149*/  149,
    /*              150*/  150,
    /*              151*/  151,
    /*              152*/  152,
    /*              153*/  153,
    /*              154*/  154,
    /*              155*/  155,
    /*              156*/  156,
    /*              157*/  157,
    /*              158*/  158,
    /*              159*/  159,
    /*          160 160*/  160,
    /*              161*/  161,
    /*              162*/  162,
    /*              163*/  163,
    /*              164*/  164,
    /*              165*/  165,
    /*              166*/  166,
    /*              167*/  167,
    /*              168*/  168,
    /*              169*/  169,
    /*              170*/  170,
    /*              171*/  171,
    /*              172*/  172,
    /*              173*/  173,
    /*              174*/  174,
    /*              175*/  175,
    /*          176 176*/  176,
    /*              177*/  177,
    /*              178*/  178,
    /*              179*/  179,
    /*              180*/  180,
    /*              181*/  181,
    /*              182*/  182,
    /*              183*/  183,
    /*              184*/  184,
    /*              185*/  185,
    /*              186*/  LBRACK,  // 219
    /*              187*/  RBRACK,  // 221
    /*              188*/  LESSTHAN,// 188
    /*              189*/  200,     // just for ease
    /*              190*/  GREATERTHAN, // 190
    /*              191*/  BACKSLASHKEY,  // 220
    /*          192 192*/  241,  // '' character
    /*              193*/  193,
    /*              194*/  194,
    /*              195*/  195,
    /*              196*/  196,
    /*              197*/  197,
    /*              198*/  198,
    /*              199*/  199,
    /*              200*/  200,
    /*              201*/  201,
    /*              202*/  202,
    /*              203*/  203,
    /*              204*/  204,
    /*              205*/  205,
    /*              206*/  206,
    /*              207*/  207,
    /*          208 208*/  208,
    /*              209*/  209,
    /*              210*/  210,
    /*              211*/  211,
    /*              212*/  212,
    /*              213*/  213,
    /*              214*/  214,
    /*              215*/  215,
    /*              216*/  216,
    /*              217*/  217,
    /*              218*/  218,
    /*              219*/  MINUSKEY,  // 189
    /*              220*/  TILDEKEY,  // 192
    /*              221*/  PLUSKEY,   // 187
    /*              222*/  222,       // '' character
    /*              223*/  223,
    /*          224 224*/  224,
    /*              225*/  225,
    /*              226*/  226,
    /*              227*/  227,
    /*              228*/  228,
    /*              229*/  229,
    /*              230*/  230,
    /*              231*/  231,
    /*              232*/  232,
    /*              233*/  233,
    /*              234*/  234,
    /*              235*/  235,
    /*              236*/  236,
    /*              237*/  237,
    /*              238*/  238,
    /*              239*/  239,
    /*          240 240*/  240,
    /*              241*/  241,
    /*              242*/  242,
    /*              243*/  243,
    /*              244*/  244,
    /*              245*/  245,
    /*              246*/  246,
    /*              247*/  247,
    /*              248*/  248,
    /*              249*/  249,
    /*              250*/  250,
    /*              251*/  251,
    /*              252*/  252,
    /*              253*/  253,
    /*              254*/  254,
    /*              255*/  255,
};


//key repeat buffer
ubyte keyNumberBufferKeys = 0;
keybufferchar keyBuffer[KEY_BufferLength];

ubyte keysToNotBuffer[] = {
    SHIFTKEY, LMOUSE_BUTTON, RMOUSE_BUTTON, MMOUSE_BUTTON,
    FLYWHEEL_UP, FLYWHEEL_DOWN, LMOUSE_DOUBLE, RMOUSE_DOUBLE,
    MMOUSE_DOUBLE,
    0};
/*=============================================================================
    Functions:
=============================================================================*/
/*-----------------------------------------------------------------------------
    Name        : keyInit
    Description : Start the keyboard module
    Inputs      : void
    Outputs     : clears the keyScanCode array
    Return      : void
----------------------------------------------------------------------------*/
void keyInit(void)
{
    keyClearAll();
}

/*-----------------------------------------------------------------------------
    Name        : keyClose
    Description : Does nothing
    Inputs      : ..
    Outputs     : ..
    Return      : ..
----------------------------------------------------------------------------*/
void keyClose(void)
{
    ;
}

/*-----------------------------------------------------------------------------
    Name        : keyPressDown
    Description : Called from Windows, sets the on and sticky bits of key
    Inputs      : key-index of key to set
    Outputs     : on and sticky bit set, repeat count updated
    Return      : void
----------------------------------------------------------------------------*/
void keyPressDown(udword key)
{
    bool bypass = FALSE, shift = FALSE;
    keyScanType originalKey = keySaveScan[key];
#if KEY_ERROR_CHECKING
    dbgAssert(key < KEY_TOTAL_KEYS);
#endif
#ifdef gshaw
    dbgMessagef("\nKey pressed = %d",key);
#endif


    if (opKeyDetour)
    {
        //key redefinition in options screen

        switch (opKeyDetour)
        {
            case 1:
                opDefineThisKey((keyindex)key);
                bypass = TRUE;
                break;
            case 2:
                if (key == SHIFTKEY)
                {
                    bypass = FALSE;
                }
                else
                {
                    shift = (keyIsHit(SHIFTKEY));

                    if (cmBuildHotKey((keyindex)key, shift))
                        bypass = TRUE;
                    else
                        bypass = FALSE;
                }
            //research is case 3!
        }
    }
    if (!bypass)
    {
        keyBufferAdd(key, keyIsHit(SHIFTKEY));
        keySaveScan[key].keypressed = 1;
        keySaveScan[key].keystick = 1;
        if (keySaveScan[key].keynumpressed < KEY_NUMPRESSED_MAX)
        {
            keySaveScan[key].keynumpressed++;
    #if KEY_VERBOSE_LEVEL >= 2
            dbgMessagef("\nkeyRepeat: repeat count of 0x%x incremented to %d", key, keySaveScan[key].keynumpressed);
    #endif
        }
        if (!(demDemoRecording | demDemoPlaying))
        {
            keyScanCode[key] = keySaveScan[key];
        }
        else if (demDemoPlaying)
        {
            if (key == ESCKEY)
            {                                               //escape while playing back a demo:stop playing demo
                keySaveScan[key] = originalKey;             //don't register the keypress
                demPlayEnd();                               //stop playing the demo
            }
        }
    }
    keyLastTimeHit = taskTimeElapsed;
}

/*-----------------------------------------------------------------------------
    Name        : keyRepeat
    Description : Increment repeat count for a key
    Inputs      : key - index of key to increment repeat count of.
    Outputs     : increments repeat count, clamping at a max.  That's it.
    Return      :
----------------------------------------------------------------------------*/
void keyRepeat(udword key)
{
#if KEY_ERROR_CHECKING
    dbgAssert(key < KEY_TOTAL_KEYS);
#endif
    keyBufferAdd(key, keyIsHit(SHIFTKEY));
    if (keySaveScan[key].keynumpressed < KEY_NUMPRESSED_MAX)
    {
        keySaveScan[key].keynumpressed++;
#if KEY_VERBOSE_LEVEL >= 2
        dbgMessagef("\nkeyRepeat: repeat count of 0x%x incremented to %d", key, keySaveScan[key].keynumpressed);
#endif
    }
    if (!(demDemoRecording | demDemoPlaying))
    {
        keyScanCode[key] = keySaveScan[key];
    }
    keyLastTimeHit = taskTimeElapsed;
}

/*-----------------------------------------------------------------------------
    Name        : keyPressUp
    Description : Called from windows, clears indexed key
    Inputs      : key-index of key to clear
    Outputs     : on bit and repeat count cleared, sticky bit remains
    Return      :
----------------------------------------------------------------------------*/
void keyPressUp(udword key)
{
    keySaveScan[key].keypressed = 0;
    keySaveScan[key].keynumpressed = 0;
#if KEY_VERBOSE_LEVEL >= 2
        dbgMessagef("\nkeyRepeat: repeat count of 0x%x set to 0", key);
#endif
    if (!(demDemoRecording | demDemoPlaying))
    {
        keyScanCode[key] = keySaveScan[key];
    }
}

/*-----------------------------------------------------------------------------
    Name        : keyAnyKeyHit
    Description : Checks to see if any key is hit
    Inputs      : void
    Outputs     : ..
    Return      : nonzero if any key pressed
----------------------------------------------------------------------------*/
bool keyAnyKeyHit(void)
{
    int i;
    keyScanType keyhit = { 0,0,0 };

    for (i=0;i<KEY_TOTAL_KEYS;i++)
        keyhit.keypressed |= keyScanCode[i].keypressed;

    return (bool)keyhit.keypressed;
}

/*-----------------------------------------------------------------------------
    Name        : keyAnyKeyStuck
    Description : Checks to see if any key is stuck, clearing the state if so.
    Inputs      : void
    Outputs     : ..
    Return      : nonzero if any key pressed
----------------------------------------------------------------------------*/
bool keyAnyKeyStuck(void)
{
    int i;
    keyScanType keyhit = { 0,0,0 };

    for (i=0;i<KEY_TOTAL_KEYS;i++)
    {
        keyhit.keystick |= keyScanCode[i].keystick;
        keySaveScan[i].keystick = keyScanCode[i].keystick = 0;
    }

    return (bool)keyhit.keystick;
}

/*-----------------------------------------------------------------------------
    Name        : keyClearAll
    Description : Clear all keys.
    Inputs      :
    Outputs     :
    Return      :
----------------------------------------------------------------------------*/
void keyClearAll(void)
{
    memset((void *)keyScanCode,0,sizeof(keyScanCode));
    memset((void *)keySaveScan,0,sizeof(keySaveScan));
    keyBufferClear();
}

/*-----------------------------------------------------------------------------
    Name        : keyClearAllStuckKeys
    Description : Clears the stick bits from all keys that are not currently
                    pressed.
    Inputs      :
    Outputs     :
    Return      :
----------------------------------------------------------------------------*/
void keyClearAllStuckKeys(void)
{
    sdword index;

    for (index = 0; index < KEY_TOTAL_KEYS; index++)
    {
        if (keyScanCode[index].keystick && (!keyScanCode[index].keypressed))
        {                                                   //if stuck but not pressed
            keyScanCode[index].keystick = 0;
        }
    }
}

/*-----------------------------------------------------------------------------
    Name        : keyBufferedKeyGet
    Description : Get a buffered keystroke for typing.
    Inputs      :
    Outputs     : bShift - was the shift key pressed when this one went down?
    Return      : ASCII key (including shift state) or 0 meaning there are no more buffered keys.
    Notes       : this function is not thread-safe
                  This function will return the shifted status of the key if
                    shift was pressed at the time this key was hit.
----------------------------------------------------------------------------*/
udword keyBufferedKeyGet(bool *bShift)
{
    udword keyToReturn, index;

    if (keyNumberBufferKeys == 0)
    {
        return(0);
    }
    keyToReturn = (udword)keyBuffer[0].key;
    *bShift = keyBuffer[0].bShift;
    keyScanCode[keyToReturn].keypressed = 0;
    keyScanCode[keyToReturn].keystick = 0;
    keyScanCode[keyToReturn].keynumpressed = 0;
    keySaveScan[keyToReturn].keypressed = 0;
    keySaveScan[keyToReturn].keystick = 0;
    keySaveScan[keyToReturn].keynumpressed = 0;

    for (index = 1; index < keyNumberBufferKeys; index++)
    {                                                       //shift rest of buffer back one
        keyBuffer[index - 1] = keyBuffer[index];
    }
    keyNumberBufferKeys--;
    return(keyToReturn);
}

/*-----------------------------------------------------------------------------
    Name        : keyBufferAdd
    Description : Add a keystroke to the key buffer
    Inputs      : key - scancode of key that was hit
                  bShift - was the shift key pressed?
    Outputs     :
    Return      :
----------------------------------------------------------------------------*/
void keyBufferAdd(udword key, bool bShift)
{
    if (strchr(keysToNotBuffer, key))
    {                                                       //don't bother storing strokes of certain special keys
        return;
    }
    if (keyNumberBufferKeys < KEY_BufferLength - 1)
    {                                                       //increase size of buffer if it's not already maxed
        keyBuffer[keyNumberBufferKeys].key = key;           //insert new key at the end
        keyBuffer[keyNumberBufferKeys].bShift = bShift;
        keyNumberBufferKeys++;
    }
}

/*-----------------------------------------------------------------------------
    Name        : keyBufferClear
    Description : Clears out the key buffer
    Inputs      : void
    Outputs     :
    Return      : void
----------------------------------------------------------------------------*/
void keyBufferClear(void)
{
    keyNumberBufferKeys = 0;
}

/*-----------------------------------------------------------------------------
    Name        : keyGermanToEnglish
    Description : This function converts german keyboard presses to english
    Inputs      : udword virtkey
    Outputs     : udword
    Parameters  : udword virtkey
    Return      : void
-----------------------------------------------------------------------------*/
udword keyGermanToEnglish(udword virtkey)
{
    return (keyTableGermanToEnglish[virtkey]);
}

/*-----------------------------------------------------------------------------
    Name        : keyFrenchToEnglish
    Description : This function converts german keyboard presses to english
    Inputs      : udword virtkey
    Outputs     : udword
    Parameters  : udword virtkey
    Return      : void
-----------------------------------------------------------------------------*/
udword keyFrenchToEnglish(udword virtkey)
{
    return (keyTableFrenchToEnglish[virtkey]);
}

/*-----------------------------------------------------------------------------
    Name        : keySpanishToEnglish
    Description : This function converts german keyboard presses to english
    Inputs      : udword virtkey
    Outputs     : udword
    Parameters  : udword virtkey
    Return      : void
-----------------------------------------------------------------------------*/
udword keySpanishToEnglish(udword virtkey)
{
    return (keyTableSpanishToEnglish[virtkey]);
}

/*-----------------------------------------------------------------------------
    Name        : keyItalianToEnglish
    Description : This function converts german keyboard presses to english
    Inputs      : udword virtkey
    Outputs     : udword
    Parameters  : udword virtkey
    Return      : void
-----------------------------------------------------------------------------*/
udword keyItalianToEnglish(udword virtkey)
{
    return (keyTableItalianToEnglish[virtkey]);
}



