Fixed bug 3188 - AZERTY keyboard support broken and inconsistent
authorSam Lantinga <slouken@libsdl.org>
Sat, 12 Aug 2017 15:41:03 -0700
changeset 1124727eb9a10002c
parent 11246 1667e6dafc85
child 11248 fe8057094a5a
Fixed bug 3188 - AZERTY keyboard support broken and inconsistent

Daniel Gibson

AZERTY keyboard layouts (which are the default layouts in France and Belgium) don't have the number keys (1, 2, ..., 9, 0) in the first row of keys, but ², &, é", ', (, -, è_, çà), = (with small differences between the France and Belgian variants). Numbers are reached via shift.

On Linux and OSX, SDL seems to use the corresponding ISO 8859-1 codes (231 for ç232 for ètc) as SDL_Keycode (but no SDK_* constants exists for those values!), while on Windows SDL seems to map those keys to SDLK_1, SDLK_2 etc, like you'd get on QWERTY.
I don't know how other platforms behave.

So we have two problems:
1. At least on Linux and OSX invalid/undefined SDL_Keycodes are returned
2. Different platforms behave differently (Windows vs Linux/OSX)

It's unclear what behavior is desired: Should SDL_* constants for those keys be introduced (and Windows behavior changed accordingly)?
Or should all platforms behave like Windows here and use the existing SDLK_1, ..., SDLK_0 keycodes?

This bug on the mailing list:
https://forums.libsdl.org/viewtopic.php?t=11555 (my post about Linux/Windows)
https://forums.libsdl.org/viewtopic.php?t=11573 (Tim Walters discovered the same problem on OSX about 1.5 weeks later).
src/events/SDL_keyboard.c
     1.1 --- a/src/events/SDL_keyboard.c	Sat Aug 12 15:21:26 2017 -0700
     1.2 +++ b/src/events/SDL_keyboard.c	Sat Aug 12 15:41:03 2017 -0700
     1.3 @@ -594,12 +594,22 @@
     1.4  SDL_SetKeymap(int start, SDL_Keycode * keys, int length)
     1.5  {
     1.6      SDL_Keyboard *keyboard = &SDL_keyboard;
     1.7 +    SDL_Scancode scancode;
     1.8  
     1.9      if (start < 0 || start + length > SDL_NUM_SCANCODES) {
    1.10          return;
    1.11      }
    1.12  
    1.13      SDL_memcpy(&keyboard->keymap[start], keys, sizeof(*keys) * length);
    1.14 +
    1.15 +    /* The number key scancodes always map to the number key keycodes.
    1.16 +     * On AZERTY layouts these technically are symbols, but users (and games)
    1.17 +     * always think of them and view them in UI as number keys.
    1.18 +     */
    1.19 +    keyboard->keymap[SDL_SCANCODE_0] = SDLK_0;
    1.20 +    for (scancode = SDL_SCANCODE_1; scancode <= SDL_SCANCODE_9; ++scancode) {
    1.21 +        keyboard->keymap[scancode] = SDLK_1 + (scancode - SDL_SCANCODE_1);
    1.22 +    }
    1.23  }
    1.24  
    1.25  void