Alex Møller
Alex Møller

Reputation: 17

Change Arduino keyboard language

I can't seem to find a way to change the keyboard layot on my Arduino USB.. I have tried changing it inside the IDE. Still doesn't work, i need a Danish layout. This is my code:

    #include <Keyboard.h>

void setup() {
  Keyboard.begin();

}

void loop(){
    delay (2000);
    Keyboard.press(KEY_LEFT_GUI);
    delay (400);
    Keyboard.press('r');
    Keyboard.releaseAll();
    delay (400);
    Keyboard.println("cmd");
    Keyboard.press(KEY_RETURN);
    delay (400);
    Keyboard.println("start chrome https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstleyVEVO");
    delay (400);
    Keyboard.press(KEY_RETURN);
    delay (400);
    Keyboard.releaseAll();
    Keyboard.press(KEY_LEFT_GUI);
    delay (400);
    Keyboard.press('r');
    delay (400);
    Keyboard.println("cmd");
    Keyboard.press(KEY_RETURN);
    Keyboard.releaseAll();
    delay (400);
    Keyboard.println("start chrome https://fakeupdate.net/win10ue/");
    delay (400);
    Keyboard.press(KEY_RETURN);
    delay (400);
    Keyboard.releaseAll();
    Keyboard.press(KEY_F11);
    
}

But my output insede cmd is "start chrome httpsÆ--www.youtube.com-watch_v´dQw4w9WgXcQ/ab?channel´RickAstleyVEVO"

Upvotes: 0

Views: 1803

Answers (2)

MRxMAG1C
MRxMAG1C

Reputation: 1

I also have a danish keyboard, and what worked for em was to put thid at the top of the script: #define HID_CUSTOM_LAYOUT #define LAYOUT_DANISH

Upvotes: 0

Ben T
Ben T

Reputation: 4946

As your PC has a Danish layout your Arduino needs to send the character for ":", "/", "?" and "&" after being mapped to a Danish layout.

This can be done be changing your Arduino code to send the character ">" in place of a colon, "-" instead of a forward slash, "_" instead of a question mark, "^" for the ampersand.

Explanation

A quick peek at the implementation of the keyboard library shows that it does not send ASCII. See keyboard.cpp at https://github.com/arduino-libraries/Keyboard/blob/master/src/Keyboard.cpp.

The relevant snippets of code are:

0x24|SHIFT,    // &
0x38,          // /
0x33|SHIFT,      // :
0x38|SHIFT,      // ?

The hex numbers here are USB HID Usage Ids. Essentially a "scan code" that is sent to the USB driver. This scan code is mapped to a character by your keyboard layout.

e.g. The Arduino keyboard library takes the ":" and sends the Usage Id 0x33 with a shift modifier over the USB connection. At the other end this usage id is mapped to a character using the keyboard layout. A Danish layout maps 0x33 with a shift modifier to "Æ".

If the Arduino keyboard library sees ">" then it sends the usage id 0x37 with a shift modifier. The Danish layout will map this to the ":" character.

References

Upvotes: 1

Related Questions