SDL Modifier Keys Messing Up

I am using SDL to implement a virtual keyboard and mouse controller which
sends events to my virtual machine. So, I initialize SDL Video, grab the
keysym and then send it over to the PS2 Emulator which then reads the ASCII
value and then prints the character on the virtual terminal.
However, whenever I press a modifier key (Shift, Ctrl, etc.), the whole
setup gets messed up.
The relevant code is:

else if (event->type==SDL_KEYDOWN || event->type==SDL_KEYUP)
{
    if(g_KbdState && event->type == SDL_KEYDOWN)
          {

        //printf("Key pressed ASCII: %d,

0x%x\n",event->key.keysym.sym,event->key.keysym.sym);
printf (“Key Pressed: %s\n”,
SDL_GetKeyName(event->key.keysym.sym));
ps2_put_keycode(g_KbdState, event->key.keysym.sym);
}
if (event->type == SDL_KEYUP)
printf (“Key Released\n\n”);
SDLMod modifier = event->key.keysym.mod;
//printf ("%d\n",modifier);
if (modifier)
{
printf (“I am a modifier\n”);
}
}

Now, the output generated when I press the keys (a, L_Shift+a, a) is given
below:

Key Pressed: a
Posted new KMIDATA to keyboard PS2 28, 0x1c
Key Released

Key Pressed: left shift
Posted new KMIDATA to keyboard PS2 18, 0x12
Key Pressed: a
I am a modifier
Posted new KMIDATA to keyboard PS2 28, 0x1c
Key Released

I am a modifier
Key Released

Key Pressed: a
Posted new KMIDATA to keyboard PS2 28, 0x1c
Key Released

The expected corresponding terminal output should be aAa yet the output is
aAA.

For some weird reason, the shift key is not released.

The PS2 Emulator I took from QEMU. The link is
http://trac.kju-app.org/browser/trunk/qemu/qemu/hw/ps2.c?rev=106.

However, I am using my own scancode matrix which I have posted below.

static const unsigned char ps2_raw3_keycode[326] = {
00, 01, 02, 03, 04, 05, 06, 07, 102, 13, 10, 11, 12, 90, 14, 15, //15
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 118, 28, 29, 30, 31, //31
41, 22, 82, 38, 37, 46, 61, 82, 70, 69, 62, 85, 65, 78, 73, 74, //47
69, 22, 30, 38, 37, 46, 54, 61, 62, 70, 76, 76, 65, 85, 73, 74, //63
30, 28, 50, 33, 35, 36, 43, 52, 51, 67, 59, 66, 75, 58, 49, 68, //79
77, 21, 45, 27, 44, 60, 42, 29, 34, 53, 26, 84, 93, 91, 54, 78, //95
14, 28, 50, 33, 35, 36, 43, 52, 51, 67, 59, 66, 75, 58, 49, 68, //111
77, 21, 45, 27, 44, 60, 42, 29, 34, 53, 26, 84, 93, 91, 14, 127, //127
00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, //143
00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, //159
00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, //175
00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, //191
00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, //207
00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, //223
00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, //239
00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, //255
00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, //271
00, 57461, 57458, 57460, 57451, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
00, //287
00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 88, 00, 89, //303
18, 57364, 20, 57367, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 //319
};

As for other control keys, the terminal stops outputting anything after I
press them.

Suggestions?