Incorrect keyboard keysyms returned with shift

I seem to be getting really weird keysym values when shift is involved.

Sometimes a single keypress returns three keysyms in a row!

I’ve included an example program at the bottom of this post. I’m using a UK keyboard on ArchLinux.

Interestingly, I’ve actually had similar problems with GLFW but haven’t investigated as thoroughly.

SDL compile-time version 1.2.14
SDL runtime version 1.2.14

Here’s an example of the good output:

// Good Output: (Press “a” “b” “c” “,” “.” keys in that order:
Pressed: 97 “a” (SDL_GetKeyName: "a"
Pressed: 98 “b” (SDL_GetKeyName: "b"
Pressed: 99 “c” (SDL_GetKeyName: "c"
Pressed: 44 “,” (SDL_GetKeyName: ","
Pressed: 46 “.” (SDL_GetKeyName: “.”

And here’s all the cases of bad input I found.

// Bad Output: Press "["
Pressed: 313 “9” (SDL_GetKeyName: "alt gr"
Pressed: 56 “8” (SDL_GetKeyName: “8”

// Bad Output: Press "]"
Pressed: 313 “9” (SDL_GetKeyName: "alt gr"
Pressed: 57 “9” (SDL_GetKeyName: “9”

// Try Shift + “]”, in this case we get the expected result
Pressed: 304 “0” (SDL_GetKeyName: "left shift"
Pressed: 93 “]” (SDL_GetKeyName: “]”

// Bad Output; Press Shift + “,” (note comma without shift is fine)
// on a UK keyboard it corresponds to “<”)
Pressed: 304 “0” (SDL_GetKeyName: "left shift"
Pressed: 313 “9” (SDL_GetKeyName: "alt gr"
Pressed: 122 “z” (SDL_GetKeyName: “z”

// Bad Output; Press Shift + “.” (note period without shift is fine)
// (on a UK keyboard it corresponds to “>”)
Pressed: 313 “9” (SDL_GetKeyName: "alt gr"
Pressed: 120 “x” (SDL_GetKeyName: “x”

// Bad Output: Press Shift + Apostrophe
// (on a UK keyboard this corresponds to “@”
// Three keysyms generated!
Pressed: 304 “0” (SDL_GetKeyName: "left shift"
Pressed: 301 “-” (SDL_GetKeyName: "caps lock"
Pressed: 313 “9” (SDL_GetKeyName: "alt gr"
Pressed: 113 “q” (SDL_GetKeyName: “q”

// Press backslash
Pressed: 313 “9” (SDL_GetKeyName: "alt gr"
Pressed: 45 “-” (SDL_GetKeyName: “-”

// Press Shift + Backslash (this is the “|” stave):
Pressed: 304 “0” (SDL_GetKeyName: "left shift"
Pressed: 313 “9” (SDL_GetKeyName: "alt gr"
Pressed: 96 “" (SDL_GetKeyName: "

// Actually pressing Alt Gr on the keyboard gives
Pressed: 306 “2” (SDL_GetKeyName: "left ctrl"
Pressed: 307 “3” (SDL_GetKeyName: “right alt”

Am I doing something wrong?

Working example:

Code:

#include <stdio.h>
#include “SDL.h”

void print_SDL_version(char* preamble, SDL_version* v)
{
printf("%s %u.%u.%u\n", preamble, v->major, v->minor, v->patch);
}
void print_SDL_versions(void)
{
SDL_version ver;

/* compiletimetime version */
SDL_VERSION(&ver);
print_SDL_version("SDL compile-time version", &ver);

/* run-time version */
ver = *SDL_Linked_Version();
print_SDL_version("SDL runtime version", &ver);

}

int main(void)
{
SDL_Surface *screen;
SDL_Event keyevent;

SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(320, 240, 32, SDL_SWSURFACE);

print_SDL_versions();
printf("SDL_EnableUNICODE = %d\n", SDL_EnableUNICODE(-1));

while (1)
{
    while (SDL_PollEvent(&keyevent))
    {
        if (keyevent.key.keysym.sym == SDLK_ESCAPE) { return 0; }
        if (keyevent.type == SDL_KEYDOWN)
        {
            printf("Pressed: %d \"%c\" "
                "(SDL_GetKeyName: \"%s\"\n",  
                keyevent.key.keysym.sym,
                keyevent.key.keysym.sym,
                SDL_GetKeyName(keyevent.key.keysym.sym));
        }

    }
}


return 0;

}

Aha! This had me stumped for days but on a hunch I just tested something seconds after posting this.

I use Synergy (http://synergy-foss.org/) to share a keyboard between a windows computer (host) and my linux laptop.

Using the laptop’s native keyboard solved the problem

It appears the issue is due the way synergy triggers key presses.

Hopefully this saves some other people some frustration! Something to add to a “troubleshooting guide”, anyway!