1616
1717from . import find_device
1818
19+ try :
20+ from typing import Sequence
21+ import usb_hid
22+ except ImportError :
23+ pass
24+
1925_MAX_KEYPRESSES = const (6 )
2026
2127
@@ -33,10 +39,10 @@ class Keyboard:
3339
3440 # No more than _MAX_KEYPRESSES regular keys may be pressed at once.
3541
36- def __init__ (self , devices ) :
42+ def __init__ (self , devices : Sequence [ usb_hid . device ]) -> None :
3743 """Create a Keyboard object that will send keyboard HID reports.
3844
39- Devices can be a list of devices that includes a keyboard device or a keyboard device
45+ Devices can be a sequence of devices that includes a keyboard device or a keyboard device
4046 itself. A device is any object that implements ``send_report()``, ``usage_page`` and
4147 ``usage``.
4248 """
@@ -64,7 +70,7 @@ def __init__(self, devices):
6470 time .sleep (1 )
6571 self .release_all ()
6672
67- def press (self , * keycodes ) :
73+ def press (self , * keycodes : int ) -> None :
6874 """Send a report indicating that the given keys have been pressed.
6975
7076 :param keycodes: Press these keycodes all at once.
@@ -90,7 +96,7 @@ def press(self, *keycodes):
9096 self ._add_keycode_to_report (keycode )
9197 self ._keyboard_device .send_report (self .report )
9298
93- def release (self , * keycodes ) :
99+ def release (self , * keycodes : int ) -> None :
94100 """Send a USB HID report indicating that the given keys have been released.
95101
96102 :param keycodes: Release these keycodes all at once.
@@ -106,21 +112,21 @@ def release(self, *keycodes):
106112 self ._remove_keycode_from_report (keycode )
107113 self ._keyboard_device .send_report (self .report )
108114
109- def release_all (self ):
115+ def release_all (self ) -> None :
110116 """Release all pressed keys."""
111117 for i in range (8 ):
112118 self .report [i ] = 0
113119 self ._keyboard_device .send_report (self .report )
114120
115- def send (self , * keycodes ) :
121+ def send (self , * keycodes : int ) -> None :
116122 """Press the given keycodes and then release all pressed keys.
117123
118124 :param keycodes: keycodes to send together
119125 """
120126 self .press (* keycodes )
121127 self .release_all ()
122128
123- def _add_keycode_to_report (self , keycode ) :
129+ def _add_keycode_to_report (self , keycode : int ) -> None :
124130 """Add a single keycode to the USB HID report."""
125131 modifier = Keycode .modifier_bit (keycode )
126132 if modifier :
@@ -141,7 +147,7 @@ def _add_keycode_to_report(self, keycode):
141147 # All slots are filled.
142148 raise ValueError ("Trying to press more than six keys at once." )
143149
144- def _remove_keycode_from_report (self , keycode ) :
150+ def _remove_keycode_from_report (self , keycode : int ) -> None :
145151 """Remove a single keycode from the report."""
146152 modifier = Keycode .modifier_bit (keycode )
147153 if modifier :
@@ -154,11 +160,11 @@ def _remove_keycode_from_report(self, keycode):
154160 self .report_keys [i ] = 0
155161
156162 @property
157- def led_status (self ):
163+ def led_status (self ) -> bytes :
158164 """Returns the last received report"""
159165 return self ._keyboard_device .last_received_report
160166
161- def led_on (self , led_code ) :
167+ def led_on (self , led_code : int ) -> bool :
162168 """Returns whether an LED is on based on the led code
163169
164170 Examples::
0 commit comments