@@ -35,15 +35,14 @@ def __init__(self, keyboard):
35
35
"""
36
36
self .keyboard = keyboard
37
37
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
45
45
if altgr :
46
- # Add altgr modifier
47
46
self .keyboard .press (self .RIGHT_ALT_CODE )
48
47
# If this is a shifted char, clear the SHIFT flag and press the SHIFT key.
49
48
if keycode & self .SHIFT_FLAG :
@@ -56,22 +55,24 @@ def write(self, string):
56
55
"""Type the string by pressing and releasing keys on my keyboard.
57
56
58
57
: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
60
59
(such as some control characters).
61
60
"""
62
61
for char in string :
63
62
# find easy ones first
64
63
keycode = self ._char_to_keycode (char )
65
64
if keycode > 0 :
66
- self ._write (char , keycode , char in self .NEED_ALTGR )
65
+ self ._write (keycode , char in self .NEED_ALTGR )
67
66
# find combined keys
68
67
elif char in self .COMBINED_KEYS :
68
+ # first key (including shift bit)
69
69
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)
71
72
char = chr (cchar & 0xFF & (~ self .ALTGR_FLAG ))
72
73
keycode = self ._char_to_keycode (char )
73
74
# assume no altgr needed for second key
74
- self ._write (char , keycode , False )
75
+ self ._write (keycode , False )
75
76
else :
76
77
raise ValueError (
77
78
"No keycode available for character {letter} ({num}/0x{num:02x})." .format (
@@ -82,10 +83,9 @@ def write(self, string):
82
83
def keycodes (self , char ):
83
84
"""Return a tuple of keycodes needed to type the given character.
84
85
85
- :param char: A single ASCII character in a string.
86
+ :param char: A single UTF8 character in a string.
86
87
:type char: str of length one.
87
88
:returns: tuple of Keycode keycodes.
88
- :raises ValueError: if ``char`` is not ASCII or there is no keycode for it.
89
89
"""
90
90
keycode = self ._char_to_keycode (char )
91
91
if keycode == 0 :
@@ -104,8 +104,7 @@ def keycodes(self, char):
104
104
def _above128char_to_keycode (self , char ):
105
105
"""Return keycode for above 128 ascii codes.
106
106
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.
109
108
110
109
:param char_val: ascii char value
111
110
:return: keycode, with modifiers if needed
0 commit comments