@@ -33,12 +33,17 @@ def __init__(self, keyboard):
33
33
"""Specify the layout for the given keyboard.
34
34
35
35
: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)
36
41
"""
37
42
self .keyboard = keyboard
38
43
39
44
def _write (self , keycode , altgr = False ):
40
45
"""Type a key combination based on shift bit and altgr bool
41
-
46
+
42
47
:param keycode: int value of the keycode, with the shift bit.
43
48
:param altgr: bool indicating if the altgr key should be pressed too.
44
49
"""
@@ -58,6 +63,11 @@ def write(self, string):
58
63
:param string: A string of ASCII characters.
59
64
:raises ValueError: if any of the characters has no keycode
60
65
(such as some control characters).
66
+
67
+ Example::
68
+
69
+ # Write abc followed by Enter to the keyboard
70
+ layout.write('abc\\ n')
61
71
"""
62
72
for char in string :
63
73
# find easy ones first
@@ -87,10 +97,26 @@ def keycodes(self, char):
87
97
:param char: A single UTF8 character in a string.
88
98
:type char: str of length one.
89
99
: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('é')
90
112
"""
91
113
keycode = self ._char_to_keycode (char )
92
114
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
+ )
94
120
95
121
codes = []
96
122
if char in self .NEED_ALTGR :
0 commit comments