Cant print \ with Arduino Leonardo on Swiss Layout

So I want to use arduino Leonardo as a macro keyboard, however every time i try to print bslash like this:

Keyboard.print(\\)

I get either $ or £. I did some research and it seems that my swiss layout keyboard has a dedicated key for this next to Y ( Z on US Layout) that isn't mapped in the Keyboard library, and I couldn't find any other way to type \ on swiss layout.
So how can I print bslash using Keyboard.h? Is there any way to print characters independent of the current Windows keyboard layout?

btw. Sorry for grammar and terrible formatting, im on the phone right now and english isn't my first language

Hi @Milan2006. Some really nice work has been done lately on the Keyboard library to allow it to work for any layout.

There is documentation about it here:

Unfortunately, there is not yet a Swiss layout, but there is documentation about adding new layouts here:

Here you can see an example of what was needed to add the German layout:

1 Like

@in0 Thanks! I didn't know you could change layouts. As it turns out, the bslash is the same key as in German layout so i don't have to create my own.

You are welcome. I'm glad that you were able to get it working. Enjoy!

You could try

Keyboard.print((char) 92)

because backslash is decimal code 92 in the ASCII set.

The problem is that a USB keyboard doesn't send ASCII. It sends a code indicating a key on the keyboard. By default, the Keyboard library translates ASCII characters to the key code they have on the US-English keyboard and then the OS translates the keycode back to a character. If the currently selected keyboard layout is not US-English the OS may translate to the wrong character.

One workaround is the KeyboardUTF8 library I wrote last year. It takes any string of UTF8 characters up to 0xFFFF (which includes backslash) and sends the right keystrokes for the specified keyboard layout.

2 Likes

Thanks John, I did not know that.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.