Skip to content

Commit a2ec178

Browse files
committed
Reorganise keyboard_layout a little
- remove char from _write() - no need to bitwise & both 0xFF and ALTGR_FLAG - no need to raise an error in _write: keycode is not 0 - fix a few docstrings
1 parent 531cf6d commit a2ec178

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

libraries/common/keyboard_layout.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
6867
elif char in self.COMBINED_KEYS:
68+
# first key (including shift bit)
6969
cchar = self.COMBINED_KEYS[char]
70-
self._write(char, (cchar >> 8), (cchar & 0xFF & self.ALTGR_FLAG))
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,8 +104,7 @@ 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

0 commit comments

Comments
 (0)