Skip to content

Instantly share code, notes, and snippets.

@VoidNoi
Last active June 17, 2024 19:48
Show Gist options
  • Save VoidNoi/4087bab5d67458a52fe22a574a5f7394 to your computer and use it in GitHub Desktop.
Save VoidNoi/4087bab5d67458a52fe22a574a5f7394 to your computer and use it in GitHub Desktop.
How to create a keyboard layout for BadCard

Basic layout

Choose an already made layout (Like: KeyboardLayout_ES.h) and copy the contents to a new file named "KeyboardLayout_*.h" where "*" is your country code in caps E.g: "KeyboardLayout_ES.h"

Now that you have your file, start by modifying extern const uint8_t KeyboardLayout_es_ES[128]

For this I'll just copy the already made tutorial in https://github.com/arduino-libraries/Keyboard/blob/master/src/KeyboardLayout.h

  == Creating your own layout ==

  In order to create your own layout file, copy an existing layout that
  is similar to yours, then modify it to use the correct keys. The
  layout is an array in ASCII order. Each entry contains a scan code,
  possibly modified by "|SHIFT" or "|ALT_GR", as in this excerpt from
  the Italian layout:

      0x35,          // bslash
      0x30|ALT_GR,   // ]
      0x2e|SHIFT,    // ^

  Do not change the control characters (those before scan code 0x2c,
  corresponding to space). Do not attempt to grow the table past DEL. Do
  not use both SHIFT and ALT_GR on the same character: this is not
  supported. Unsupported characters should have 0x00 as scan code.

  For a keyboard with an ISO physical layout, use the scan codes below:
  
      +---+---+---+---+---+---+---+---+---+---+---+---+---+-------+
      |35 |1e |1f |20 |21 |22 |23 |24 |25 |26 |27 |2d |2e |BackSp |
      +---+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-----+
      | Tab |14 |1a |08 |15 |17 |1c |18 |0c |12 |13 |2f |30 | Ret |
      +-----++--++--++--++--++--++--++--++--++--++--++--++--++    |
      |CapsL |04 |16 |07 |09 |0a |0b |0d |0e |0f |33 |34 |31 |    |
      +----+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+---+----+
      |Shi.|32 |1d |1b |06 |19 |05 |11 |10 |36 |37 |38 |  Shift   |
      +----+---++--+-+-+---+---+---+---+---+--++---+---++----+----+
      |Ctrl|Win |Alt |                        |AlGr|Win |Menu|Ctrl|
      +----+----+----+------------------------+----+----+----+----+
      
  The ANSI layout is identical except that key 0x31 is above (rather
  than next to) Return, and there is not key 0x32.

TLDR: Modify the scan codes to make it match in your keyboard according to the physical layout that is shown

E.g: In spanish layout + key would be 0x30 but in the french layout it would be 0x2e|SHIFT

Note that dead keys should have the 0x00 code and they will be defined on the next step

Missing keys

Now that you have the basic keys there might me some that are not in the list, not to worry, we are going to put those at the top.

  1. Define the missing key and give it the corresponding name, then assign the scan code like in the previous step but this time do it like this: #define KEY_MISSING (136+0x00) where 0x00 is the corresponding key code
    - Do not add the SHIFT modifier here!
  2. Modify extern const char16_t KeyboardLayout_es_ES_special_characters[AMOUNT_OF_SPECIAL_CHARS] and add the keys you defined between u'', separated by a comma like u'ñ',
  3. Modify extern const uint16_t KeyboardLayout_es_ES_special_keycodes[AMOUNT_OF_SPECIAL_CHARS] and add the defined key separated by a comma in the same order you did in the last step.
    - If the key requires the use of SHIFT, then use U16SHIFT and add it like this: KEY_MISSING | U16SHIFT,
    - Just like before, if the key needs to use both SHIFT and ALT_GR then do not add it, as this is not supported
  4. Modify AMOUNT_OF_SPECIAL_CHARS and add the amount of keys that are on the previous variables you modified

Last changes

You're almost done, now you just need to make sure to name everything properly according to your language

For this search each stance of es_ES being used and modify it with your language code + your country code

E.g: da_DK for the danish (Denmark) keyboard layout

You're also going to need to modify class KeyboardLayout_ES and the two other instances that KeyboardLayout_ES is being called with your country code

E.g KeyboardLayout_DK for the danish (Denmark) keyboard layout

With this you should be done and you can either create a pull request on the BadCard repository or send me a message on matrix @voidnoi:constellatory.net and I'll add it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment