Skip to content

Commit f75ba75

Browse files
committed
add back examples in KeyboardLayoutBase, and raise in keycodes()
1 parent 712147e commit f75ba75

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

adafruit_hid/keyboard_layout_base.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,17 @@ def __init__(self, keyboard):
3333
"""Specify the layout for the given keyboard.
3434
3535
:param keyboard: a Keyboard object. Write characters to this keyboard when requested.
36+
37+
Example::
38+
39+
kbd = Keyboard(usb_hid.devices)
40+
layout = KeyboardLayout(kbd)
3641
"""
3742
self.keyboard = keyboard
3843

3944
def _write(self, keycode, altgr=False):
4045
"""Type a key combination based on shift bit and altgr bool
41-
46+
4247
:param keycode: int value of the keycode, with the shift bit.
4348
:param altgr: bool indicating if the altgr key should be pressed too.
4449
"""
@@ -58,6 +63,11 @@ def write(self, string):
5863
:param string: A string of ASCII characters.
5964
:raises ValueError: if any of the characters has no keycode
6065
(such as some control characters).
66+
67+
Example::
68+
69+
# Write abc followed by Enter to the keyboard
70+
layout.write('abc\\n')
6171
"""
6272
for char in string:
6373
# find easy ones first
@@ -87,10 +97,26 @@ def keycodes(self, char):
8797
:param char: A single UTF8 character in a string.
8898
:type char: str of length one.
8999
:returns: tuple of Keycode keycodes.
100+
:raises ValueError: if there is no keycode for ``char``.
101+
102+
Examples::
103+
104+
# Returns (Keycode.TAB,)
105+
keycodes('\t')
106+
# Returns (Keycode.A,)
107+
keycode('a')
108+
# Returns (Keycode.SHIFT, Keycode.A)
109+
keycode('A')
110+
# Raises ValueError with a US layout because it's an unknown character
111+
keycode('é')
90112
"""
91113
keycode = self._char_to_keycode(char)
92114
if keycode == 0:
93-
return []
115+
raise ValueError(
116+
"No keycode available for character {letter} ({num}/0x{num:02x}).".format(
117+
letter=repr(char), num=ord(char)
118+
)
119+
)
94120

95121
codes = []
96122
if char in self.NEED_ALTGR:

adafruit_hid/keyboard_layout_us.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,5 @@ class KeyboardLayoutUS(KeyboardLayoutBase):
168168
b"\x4c" # DEL DELETE (called Forward Delete in usb.org document)
169169
)
170170

171+
171172
KeyboardLayout = KeyboardLayoutUS

0 commit comments

Comments
 (0)