Skip to content

Commit 085467f

Browse files
committed
repport new changes from layouts
1 parent 45ee7e0 commit 085467f

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

adafruit_hid/keyboard_layout_base.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"""
88

99

10-
__version__ = "2.0.0-auto.0"
11-
__repo__ = "https://github.com/Neradoc/Circuitpython_Keyboard_Layouts.git"
10+
__version__ = "0.0.0-auto.0"
11+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HID.git"
1212

1313

1414
class KeyboardLayoutBase:
@@ -35,15 +35,14 @@ def __init__(self, keyboard):
3535
"""
3636
self.keyboard = keyboard
3737

38-
def _write(self, char, keycode, altgr=False):
39-
if keycode == 0:
40-
raise ValueError(
41-
"No keycode available for character {letter} ({num}/0x{num:02x}).".format(
42-
letter=repr(char), num=ord(char)
43-
)
44-
)
38+
def _write(self, keycode, altgr=False):
39+
"""Type a key combination based on shift bit and altgr bool
40+
41+
:param keycode: int value of the keycode, with the shift bit.
42+
:param altgr: bool indicating if the altgr key should be pressed too.
43+
"""
44+
# Add altgr modifier if needed
4545
if altgr:
46-
# Add altgr modifier
4746
self.keyboard.press(self.RIGHT_ALT_CODE)
4847
# If this is a shifted char, clear the SHIFT flag and press the SHIFT key.
4948
if keycode & self.SHIFT_FLAG:
@@ -56,22 +55,24 @@ def write(self, string):
5655
"""Type the string by pressing and releasing keys on my keyboard.
5756
5857
:param string: A string of ASCII characters.
59-
:raises ValueError: if any of the characters are not ASCII or have no keycode
58+
:raises ValueError: if any of the characters has no keycode
6059
(such as some control characters).
6160
"""
6261
for char in string:
6362
# find easy ones first
6463
keycode = self._char_to_keycode(char)
6564
if keycode > 0:
66-
self._write(char, keycode, char in self.NEED_ALTGR)
65+
self._write(keycode, char in self.NEED_ALTGR)
6766
# find combined keys
68-
elif char in self.COMBINED_KEYS:
69-
cchar = self.COMBINED_KEYS[char]
70-
self._write(char, (cchar >> 8), (cchar & 0xFF & self.ALTGR_FLAG))
67+
elif ord(char) in self.COMBINED_KEYS:
68+
# first key (including shift bit)
69+
cchar = self.COMBINED_KEYS[ord(char)]
70+
self._write(cchar >> 8, cchar & self.ALTGR_FLAG)
71+
# second key (removing the altgr bit)
7172
char = chr(cchar & 0xFF & (~self.ALTGR_FLAG))
7273
keycode = self._char_to_keycode(char)
7374
# assume no altgr needed for second key
74-
self._write(char, keycode, False)
75+
self._write(keycode, False)
7576
else:
7677
raise ValueError(
7778
"No keycode available for character {letter} ({num}/0x{num:02x}).".format(
@@ -82,10 +83,9 @@ def write(self, string):
8283
def keycodes(self, char):
8384
"""Return a tuple of keycodes needed to type the given character.
8485
85-
:param char: A single ASCII character in a string.
86+
:param char: A single UTF8 character in a string.
8687
:type char: str of length one.
8788
:returns: tuple of Keycode keycodes.
88-
:raises ValueError: if ``char`` is not ASCII or there is no keycode for it.
8989
"""
9090
keycode = self._char_to_keycode(char)
9191
if keycode == 0:
@@ -104,16 +104,15 @@ def keycodes(self, char):
104104
def _above128char_to_keycode(self, char):
105105
"""Return keycode for above 128 ascii codes.
106106
107-
Since the values are sparse, this may be more space efficient than bloating the table above
108-
or adding a dict.
107+
A character can be indexed by the char itself or its int ord() value.
109108
110109
:param char_val: ascii char value
111110
:return: keycode, with modifiers if needed
112111
"""
113-
if char in self.HIGHER_ASCII:
114-
return self.HIGHER_ASCII[char]
115112
if ord(char) in self.HIGHER_ASCII:
116113
return self.HIGHER_ASCII[ord(char)]
114+
if char in self.HIGHER_ASCII:
115+
return self.HIGHER_ASCII[char]
117116
return 0
118117

119118
def _char_to_keycode(self, char):

0 commit comments

Comments
 (0)