Author Topic: Keyboard library doesnt work with non-us setups  (Read 16204 times)

joakim

  • Newbie
  • *
  • Posts: 4
Keyboard library doesnt work with non-us setups
« on: August 19, 2014, 01:42:22 pm »
Due to the fact that I am running a non-US charset on my computer, I somehow need to modify the keyboard library:

Code: [Select]
DigiKeyboard.println("http://www.digistump.com"); => httpÖ--www.digistump.com
So what would be the appropriate way to work around this?

I have looked through both  DigiKeyboard.h & scancode-ascii-table.h. Tried to mix up the definitions, but without result. Am I on the right track here?

Many thanks
« Last Edit: August 19, 2014, 01:44:41 pm by joakim »

gniebseh

  • Newbie
  • *
  • Posts: 2
Re: Keyboard library doesnt work with non-us setups
« Reply #1 on: August 24, 2014, 12:36:14 am »
I have this issue too!
Did you get to work properly with a non-us layout? I think this affects a lot of people..

I saw that the teensy has support for many other layouts including Swedish.
https://github.com/PaulStoffregen/cores/blob/master/teensy/keylayouts.h
(the digispark defaults match the HID Usage Tables 1.12 document: http://www.usb.org/developers/devclass_docs/Hut1_12v2.pdf so I guess this is useful somehow.. :) )

I shoved in those definitions from the Swedish layout but it didn't help.

Anyone have any tips?

I saw duckythescientist's post on Dvorak layout so I used his dvorak layout in my DigiKeyboard.h http://digistump.com/board/index.php?topic=99.0

Quote
  DigiKeyboard.println("abcdefghijklmnop {}[]()-+;:");

Output: abcdefghijklmnop Å=+Ö

There is no difference in the output from the US layout (both are wrong in the exact same way), this cannot be right! This must be a bug.
Whats going on?

Edit:
So println is not affected by DigiKeyboard.h?
sendKeyStroke works as expected, I can output all the things I want.
Will I have to write my own print function that loops over every character in my string and sends each one with sendKeyStroke? :(

int i;
for(i = 0; i < 56; i++){
  DigiKeyboard.print(i);
  DigiKeyboard.println(" ");
  DigiKeyboard.sendKeyStroke(i);
  DigiKeyboard.sendKeyStroke(i, MOD_SHIFT_RIGHT);  //and MOD_ALT_RIGHT
  DigiKeyboard.println("");
  DigiKeyboard.delay(50);
}
« Last Edit: August 24, 2014, 02:01:51 am by gniebseh »

joakim

  • Newbie
  • *
  • Posts: 4
Re: Keyboard library doesnt work with non-us setups
« Reply #2 on: August 24, 2014, 02:07:32 am »
Hi!

No success here yet. Actually I also try to make it work with a Swedish layout, so..hej på dig :)

Dont think its a bug as key definitions are only used when invoking the sendKeyStroke methods, like sendKeyStroke(KEY_DEL, MOD_ALT_LEFT| MOD_CONTROL_LEFT); rather than when using the print("Hello world") or println("...")-methods. This is due to the fact that whenever you try to run the print or println method, your strings will be translated char by char into appropriate hex values and then matched against the contents inside the file scancode-ascii-table.h (same directory as the DigiKeyboard file).
Knowing very little C I have set out to try and reverse the table and figure out what I need to replace and where to make it work, but to be honest I am quite far from succeeding right now.

Perhaps someone else here knows how one could regenerate the scancode-ascii-table.h-file and we'd all be happy?

Let me know what how things turn out for you.

Thank you
« Last Edit: August 24, 2014, 04:33:48 am by joakim »

gogol

  • Sr. Member
  • ****
  • Posts: 398
Re: Keyboard library doesnt work with non-us setups
« Reply #3 on: August 25, 2014, 05:39:49 am »
The problem isn't big, but involves rewrite of the library.  The shortcoming of the library is, that the library is only intended, to work with 7-bit code and not with 8-bit code like ISO/IEC 8859-x (or even better UTF8).

In scan-code-ascii-table.h the comment:
Quote
// Format: most signifficant bit indicates if scan code should be sent with shift modifier
// remaining 7 bits are to be used as scan code number.
tells about the problem.

If you wish to print german characters like öÖäÄüÜß  or swedish ones like mentioned above, they are represented by 8bit.  So there is no way, to encode, if the scan-code needs to be entered together witrh the shift-modifier or not.

That is the ugly piece of the code:
Code: [Select]
size_t write(uint8_t chr) {
    uint8_t data = pgm_read_byte_near(ascii_to_scan_code_table + (chr - 8));
    sendKeyStroke(data & 0b01111111, data >> 7 ? MOD_SHIFT_RIGHT : 0);
    return 1;
  }

So the question of joakim in the first post is very easy to fix:

For the /-character enter    /* ASCII:  47 */ 164,  instead of  /* ASCII:  47 */ 56,
and for the :-character enter    /* ASCII:  58 */ 183,  instead of  /* ASCII:  58 */ 179,

and the URL will be entered with a german keyboard driver as expected.

What needs to be done:

Some kind of a reverse keyboard driver, which replaces characters with the associated scan-code, where the char is located on this countries keyboard.

That table needs a full 8-bit lookup column, and instead of one byte (with shift-modifier encoded in the highest bit) another byte, which allows the mapping of all modifiers. So you are able to enter characters like € @ \ ~ and others, which need for example the modfiler Left-Ctrl and  Left-Alt together on the german keyboard.

The best implementation would be -by my opinion- a language dependent h-file.

joakim

  • Newbie
  • *
  • Posts: 4
Re: Keyboard library doesnt work with non-us setups
« Reply #4 on: August 25, 2014, 01:45:25 pm »
Hi Gogol,

Thanks for replying!

Quick question - do you mind telling me how you figured which chars are which in the ASCII table?
If so, I guess I could regenerate it for my layout as good as possible until me or someone else finds time to look into modifying the write()-function to work with 8 bits?

gniebseh

  • Newbie
  • *
  • Posts: 2
Re: Keyboard library doesnt work with non-us setups
« Reply #5 on: August 25, 2014, 02:35:59 pm »
I'm not very good with the math and programming, but I did figure out the swedish scancodes for a couple characters.
I wish I had a backslash, though its fairly complete with the most usable ones for me right now..
Heres stuff I've found so far:
  /* ASCII:  34 */ 159, // doublequotes
  /* ASCII:  39 */ 49, // quote
  /* ASCII:  40 */ 165, // opn parenthesis
  /* ASCII:  41 */ 166, // cls parenthesis
  /* ASCII:  42 */ 177, // wildcard
  /* ASCII:  43 */ 45,   // plus
  /* ASCII:  44 */ 54,   // comma
  /* ASCII:  45 */ 56,   // minus
  /* ASCII:  47 */ 164,   // slash
  /* ASCII:  59 */ 182,  // semicolon
  /* ASCII:  58 */ 183,  // colon
  /* ASCII:  63 */ 173,   //underline
   /* ASCII:  61 */ 167,  // equals
  /* ASCII:  95 */ 184,  // questionmark
(to be placed in the correct line ordered by the "ASCII: XX" number)

I used a semi-naive approach and printed all characters and named them by stealing the table from http://www.ascii-code.com/ and running:
cat ascii-code | awk '{ print "DigiKeyboard.println(\""$1" - "$7" "$8" - "$5"\");" }'
outputs runnable code like:
DigiKeyboard.println("35 - Number  - #");
DigiKeyboard.println("36 - Dollar  - $");
DigiKeyboard.println("37 - Procenttecken  - %");

If I printed Number-character # but the Dollar-sign $ was printed then I knew # has the scancode in the scancode-ascii-table.h for $. I would simply take that value and place it in the table line for the $ sign instead giving a correctly printed character. (# == /* ASCII:  35 */ XXX, and $ == /* ASCII:  36 */ XXX,).

A tip is to output to cat > textoutput in a console terminal since that wont translate characters to control-codes/shortcuts that mess up your windows and start writing the characters to other applications (happened way too many times).

I thought I would write some of my findings so anyone who googles this issue would get some hints on doing it the straight-forward but inefficient way.

Anyway, grateful for a backslash and a proper solution to this ! :)
« Last Edit: August 25, 2014, 02:39:00 pm by gniebseh »

gogol

  • Sr. Member
  • ****
  • Posts: 398
Re: Keyboard library doesnt work with non-us setups
« Reply #6 on: August 26, 2014, 12:03:40 am »
Hi Gogol,

Thanks for replying!

Quick question - do you mind telling me how you figured which chars are which in the ASCII table?
If so, I guess I could regenerate it for my layout as good as possible until me or someone else finds time to look into modifying the write()-function to work with 8 bits?

That page has some useful tips:

The ugly thing is, that USB scan codes are other than PS2 scancodes.  Windows for example has just another mapping layer on top of USB and translates USB scancodes to PS2-scancodes, which than are translated to Keystroke via language mapping.
So many tools showing scancodes, are still showing PS2 scancodes, whenever you are working with an USB keyboard. 
But as we are implemeting real hardware, we have to talk USB HID codes!

The odd thing here is, that most tables for Scancodes still refer to US keyboards, when they are naming the keys (instead of just showing a typical 101/102 keyboard with numbered keys before the table)






rebane2001

  • Newbie
  • *
  • Posts: 2
Re: Keyboard library doesnt work with non-us setups
« Reply #7 on: December 19, 2016, 07:59:08 am »
I made an account on this site just to reply to this post 
A semi-fix for it is to set your keyboard layout to US, but type out what you want to type as if you were using your own layout's keyboard
That way it will press the correct characters

MaMe82

  • Newbie
  • *
  • Posts: 9
Re: Keyboard library doesnt work with non-us setups
« Reply #8 on: February 07, 2017, 04:42:16 am »
I got a fix by outsourcing the keyboard layout problem to RubberDucky DuckEncoder.
The script uses a raw RubberDucky payload (generated with respect to choosen keyboard layout) and creates a DigiSpark Arduino Sketch source from it.

This additionally deals with the problem of moving long keyboard strings to FLASH memory and includes flashing status LED after payload execution.

The generated source only has to be moved to Arduino IDE by copy and paste.

For details: https://github.com/mame82/duck2spark

Fardenco

  • Newbie
  • *
  • Posts: 8
Re: Keyboard library doesnt work with non-us setups
« Reply #9 on: February 18, 2017, 04:35:07 pm »