adafruit / Adafruit_CircuitPython_HID

USB Human Interface Device drivers.
MIT License
364 stars 106 forks source link

Add keyboard_layout_base and switch keyboard_layout_us to it #84

Closed Neradoc closed 2 years ago

Neradoc commented 2 years ago

This is the KeyboardLayoutBase class used in the layout bundle.

With the Ñ example:

In the Keyboard Layout bundle, every layout class is called KeyboardLayout, to make changing layout easier without having a different class name in every file, and have to guess or lookup the class name (is it CamelCase of the module name ? Is it US or Us ?). For compatibility, I added KeyboardLayout = KeyboardLayoutUS in the US keyboard layout. Importing multiple layouts is possible by using the full module.KeyboardLayout name, or import KeyboardLayout as ...

I'm not quite sure if I did the documentation correctly, I would love guidance on that. KeyboardLayoutBase is not for end-user use, but it's now the class that contains the method calls of the layout and therefore their documentation.

Neradoc commented 2 years ago

I've pushed an update with a bunch more docs and rebased, and the type annotations.

dhalbert commented 2 years ago

It is the interior \n that is disappearing: with this change:

Adafruit CircuitPython 7.0.0-579-g9da541ed2-dirty on 2021-10-26; Adafruit CircuitPlayground Express with samd21g18
>>> from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
>>> from adafruit_hid.keyboard import Keyboard
>>> import usb_hid
>>> k = Keyboard(usb_hid.devices)
>>> l = KeyboardLayoutUS(k)
>>> l.write("abc\ndef\n")
>>> abcdef
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'abcdef' is not defined
>>> 

Latest stock adafruit/main:

Adafruit CircuitPython 7.0.0-579-g9da541ed2 on 2021-10-26; Adafruit CircuitPlayground Express with samd21g18
>>> from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
>>> from adafruit_hid.keyboard import Keyboard
>>> import usb_hid
>>> k = Keyboard(usb_hid.devices)
>>> l = KeyboardLayoutUS(k)
>>> l.write("abc\ndef\n")
>>> abc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'abc' is not defined
>>> def
... 
dhalbert commented 2 years ago

This is really odd. Now I cannot reproduce this.

dhalbert commented 2 years ago

This is really odd. Now I cannot reproduce this.

It's intermittent; still trying.

dhalbert commented 2 years ago

There's some kind of race condition, unrelated to this PR. I'm seeing this kind of thing intermittently, on both stock 7.0.0 and with this PR. So nothing to do with the PR.

>>> l.write("abc\ndeg\n")
>>> abcdeg
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'abcdeg' is not defined
>>> time.sleep(1);l.write("abc\ndeg\n")
>>> abc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'abc' is not defined
>>> deg
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'deg' is not defined
>>>