2

With reference to the Arduino Keyboard library, it does not have a keymap for the UK keyboard. I have tried to find a keyboard map that shows the hex value for the UK keyboard but I didn't have any luck.

If you look in the Arduino Keyboard library: Arduino\libraries\Keyboard\scr\KeyboardLayout_en_US.cpp it has some other languages but no keyboard layout for a UK keyboard.

It does say somewhere I think on GitHub that a keyboard layout file can be added, but where can I get that file or how can I create that file? Has anyone already created a KeyboardLayout_en_UK.cpp file I could use, please?

2
  • 1
    Edgar Bonet, a regular contributor here, has some additional layouts at github.com/arduino-libraries/Keyboard/tree/master/src - however the UK keyboard doesn't seem to be present. He can probably help you, no doubt he will see this post shortly.
    – Nick Gammon
    Jun 17, 2023 at 10:44
  • Thanks for useful info
    – Susan
    Jun 17, 2023 at 13:42

2 Answers 2

1

As an example of what you might do ...

First copy the file KeyboardLayout_en_US.cpp and rename it as KeyboardLayout_en_UK.cpp.

Most of the entries will be the same. Now looking at the layout in https://github.com/arduino-libraries/Keyboard/blob/1.0.4/src/KeyboardLayout.h we see this:

  +---+---+---+---+---+---+---+---+---+---+---+---+---+-------+
  |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 third key along the top row of my (US) keyboard (below the function keys) has @ on top and 2 on the bottom. It has the code 1f in the chart.

In the US layout file we see, in part:

0x2c,          // ' '        0x20
0x1e|SHIFT,    // !          0x21
0x34|SHIFT,    // "          0x22
0x20|SHIFT,    // #          0x23
0x21|SHIFT,    // $          0x24
0x22|SHIFT,    // %          0x25
0x24|SHIFT,    // &          0x26
0x34,          // '          0x27
0x26|SHIFT,    // (          0x28
0x27|SHIFT,    // )          0x29
0x25|SHIFT,    // *          0x2a
0x2e|SHIFT,    // +          0x2b
0x36,          // ,          0x2c
0x2d,          // -          0x2d
0x37,          // .          0x2e
0x38,          // /          0x2f
0x27,          // 0          0x30
0x1e,          // 1          0x31
0x1f,          // 2          0x32
0x20,          // 3          0x33
0x21,          // 4          0x34
0x22,          // 5          0x35
0x23,          // 6          0x36
0x24,          // 7          0x37
0x25,          // 8          0x38
0x26,          // 9          0x39
0x33|SHIFT,    // :          0x3a
0x33,          // ;          0x3b
0x36|SHIFT,    // <          0x3c
0x2e,          // =          0x3d
0x37|SHIFT,    // >          0x3e
0x38|SHIFT,    // ?          0x3f
0x1f|SHIFT,    // @          0x40

I've put the positions of each entry on the right (the offset from the start of the array).

So looking at that table, when the scan code 1f arrives, and SHIFT is pressed, we get 0x40 which is indeed @ in the ASCII table.

Similarly, 1f without SHIFT gives us 0x32 which is 2 in the ASCII table.

Now in your case I believe that key gives " when SHIFT is pressed, so the third line from the top in my example changes from:

0x34|SHIFT,    // "          0x22

to:

0x1f|SHIFT,    // "          0x22

And likewise for the other keys. So, the @ symbol moves to where the quote is in the US keyboard. So change the last line in my example from:

0x1f|SHIFT,    // @          0x40

to:

0x34|SHIFT,    // "          0x40

Those two changes "swap" the locations of the " and the @ symbols.


As noted in the comments, keys marked ¬ and £ do not have a 7-bit ASCII representation so you will not be able to put them in the table (there is no entry for them).

1
  • Thanks for this, I'm just getting down to figuring this out myself and this info is really helpful.
    – Susan
    Jun 18, 2023 at 16:49
2

I am not aware of a UK layout file for the Arduino Keyboard library. The library itself does not have such a layout, and a Web search returned nothing. You may consider creating this layout yourself. The documentation of Keyboard.begin() states:

Custom layouts can be created by copying and modifying an existing layout. See the instructions in the Keyboard library’s KeyboardLayout.h file.

The file KeyboardLayout.h does indeed have detailed instructions for this. Are they clear enough? If not, please tell me: I wrote those instructions, and I may try to update them if something in them is confusing.

Do you feel like trying to create this layout yourself? If so, it would be nice if you could submit it to the official library as a pull request. This way other users of the library may benefit from your work. If you don't feel like doing it, I may give it a try (I have already created a few of them). In this case, I would appreciate if you could submit an issue, just to show the maintainers that there is some interest in having the library provide this layout.

6
  • Thanks for your help, I guess I will have a go at creating a KeyboardLayout_en_UK.cpp file
    – Susan
    Jun 17, 2023 at 13:44
  • 1
    I'd suggest that the one addition which could usefully be made to those instructions is the explicit statement that this only supports 7-bit ASCII. So while a UK keyboard has keys marked ¬ and £, there are not corresponding ASCII codepoints. Jun 17, 2023 at 19:21
  • 1
    @MarkMorganLloyd: “7-bit ASCII” is a pleonasm. Would the sentence “Non-ACSII characters are not supported.”, added to the end of the first paragraph, be suitable? Or should I add “anything outside the range NUL..DEL” in parentheses to explain “Non-ACSII characters”? Jun 17, 2023 at 20:08
  • 1
    @EdgarBonet Either of those forms should do, but remember that a lot of people don't really understand that various codepages etc. based on "extended ASCII" are well outside the original ASCII specification. My experience is that the terms "7-bit ASCII" and "8-bit Extended ASCII" are well-understood, even though I know at least as well as you do that neither is strictly correct. Jun 17, 2023 at 20:27
  • 1
    @MarkMorganLloyd: OK. I submitted the clarification in a pull request Jun 17, 2023 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.