6060import audiomp3
6161import usb_hid
6262from adafruit_hid .keyboard import Keyboard
63- from adafruit_hid .keycode import Keycode
63+ from adafruit_hid .keyboard_layout_base import KeyboardLayoutBase
6464from adafruit_hid .keyboard_layout_us import KeyboardLayoutUS
65+ from adafruit_hid .keycode import Keycode
6566from adafruit_hid .consumer_control import ConsumerControl
6667from adafruit_hid .consumer_control_code import ConsumerControlCode
6768from adafruit_hid .mouse import Mouse
8081 from typing import Tuple , Optional , Union , Iterator
8182 from neopixel import NeoPixel
8283 from keypad import Keys
83- import adafruit_hid
84+ import adafruit_hid # pylint:disable=ungrouped-imports
8485except ImportError :
8586 pass
8687
9394ROTATED_KEYMAP_270 = (9 , 6 , 3 , 0 , 10 , 7 , 4 , 1 , 11 , 8 , 5 , 2 )
9495
9596
97+ keycodes = Keycode
98+ """Module level Keycode class, to be changed when initing Macropad with a different language"""
99+
100+
96101class _PixelMapLite :
97102 """Generate a pixel map based on a specified order. Designed to work with a set of 12 pixels,
98103 e.g. the MacroPad keypad LEDs.
@@ -163,7 +168,7 @@ def brightness(self, value: float) -> None:
163168 self ._pixels .brightness = value
164169
165170
166- # pylint: disable=too-many-lines
171+ # pylint: disable=too-many-lines, disable=invalid-name, too-many-instance-attributes, too-many-public-methods, too-many-arguments
167172class MacroPad :
168173 """
169174 Class representing a single MacroPad.
@@ -182,6 +187,14 @@ class MacroPad:
182187 channels. Defaults to 1.
183188 :param int midi_out_channel: The MIDI output channel. Defaults to 1.
184189
190+ :param type[KeyboardLayoutBase] layout_class: Class for the keyboard layout, to setup an
191+ international or alternative keyboard. Defaults
192+ to KeyboardLayoutUS from adafruit_hid.
193+ :param type[Keycode] keycode_class: Class used for the keycode names provided by
194+ adafruit_macropad.Keycode. Defaults to the standard Keycode
195+ from adafruit_hid.
196+
197+
185198 The following shows how to initialise the MacroPad library with the board rotated 90 degrees,
186199 and the MIDI channels both set to 1.
187200
@@ -192,9 +205,43 @@ class MacroPad:
192205 macropad = MacroPad(rotation=90, midi_in_channel=1, midi_out_channel=1)
193206 """
194207
195- # pylint: disable=invalid-name, too-many-instance-attributes, too-many-public-methods
208+ Keycode = Keycode
209+ """
210+ The contents of the Keycode module are available as a property of MacroPad. This includes all
211+ keycode constants available within the Keycode module, which includes all the keys on a
212+ regular PC or Mac keyboard.
213+
214+ Remember that keycodes are the names for key _positions_ on a US keyboard, and may not
215+ correspond to the character that you mean to send if you want to emulate non-US keyboard.
216+
217+ For usage example, see the ``keyboard`` documentation in this library.
218+ """
219+
220+ ConsumerControlCode = ConsumerControlCode
221+ """
222+ The contents of the ConsumerControlCode module are available as a property of MacroPad.
223+ This includes the available USB HID Consumer Control Device constants. This list is not
224+ exhaustive.
225+
226+ For usage example, see the ``consumer_control`` documentation in this library.
227+ """
228+
229+ Mouse = Mouse
230+ """
231+ The contents of the Mouse module are available as a property of MacroPad. This includes the
232+ ``LEFT_BUTTON``, ``MIDDLE_BUTTON``, and ``RIGHT_BUTTON`` constants. The rest of the
233+ functionality of the ``Mouse`` module should be used through ``macropad.mouse``.
234+
235+ For usage example, see the ``mouse`` documentation in this library.
236+ """
237+
196238 def __init__ (
197- self , rotation : int = 0 , midi_in_channel : int = 1 , midi_out_channel : int = 1
239+ self ,
240+ rotation : int = 0 ,
241+ midi_in_channel : int = 1 ,
242+ midi_out_channel : int = 1 ,
243+ layout_class : type [KeyboardLayoutBase ] = KeyboardLayoutUS ,
244+ keycode_class : type [Keycode ] = Keycode ,
198245 ):
199246
200247 if rotation not in (0 , 90 , 180 , 270 ):
@@ -257,6 +304,12 @@ def _keys_and_pixels(
257304 self ._keyboard_layout = None
258305 self ._consumer_control = None
259306 self ._mouse = None
307+ self ._layout_class = layout_class
308+ self .Keycode = keycode_class
309+ # pylint:disable=global-statement
310+ global keycodes
311+ keycodes = keycode_class
312+ # pylint:enable=global-statement
260313
261314 # Define MIDI:
262315 try :
@@ -271,36 +324,6 @@ def _keys_and_pixels(
271324 # No MIDI ports available.
272325 self ._midi = None
273326
274- Keycode = Keycode
275- """
276- The contents of the Keycode module are available as a property of MacroPad. This includes all
277- keycode constants available within the Keycode module, which includes all the keys on a
278- regular PC or Mac keyboard.
279-
280- Remember that keycodes are the names for key _positions_ on a US keyboard, and may not
281- correspond to the character that you mean to send if you want to emulate non-US keyboard.
282-
283- For usage example, see the ``keyboard`` documentation in this library.
284- """
285-
286- ConsumerControlCode = ConsumerControlCode
287- """
288- The contents of the ConsumerControlCode module are available as a property of MacroPad.
289- This includes the available USB HID Consumer Control Device constants. This list is not
290- exhaustive.
291-
292- For usage example, see the ``consumer_control`` documentation in this library.
293- """
294-
295- Mouse = Mouse
296- """
297- The contents of the Mouse module are available as a property of MacroPad. This includes the
298- ``LEFT_BUTTON``, ``MIDDLE_BUTTON``, and ``RIGHT_BUTTON`` constants. The rest of the
299- functionality of the ``Mouse`` module should be used through ``macropad.mouse``.
300-
301- For usage example, see the ``mouse`` documentation in this library.
302- """
303-
304327 @property
305328 def pixels (self ) -> Optional [_PixelMapLite ]:
306329 """Sequence-like object representing the twelve NeoPixel LEDs in a 3 x 4 grid on the
@@ -501,7 +524,7 @@ def keyboard_layout(self) -> adafruit_hid.keyboard_layout_base.KeyboardLayoutBas
501524 """
502525 if self ._keyboard_layout is None :
503526 # This will need to be updated if we add more layouts. Currently there is only US.
504- self ._keyboard_layout = KeyboardLayoutUS (self .keyboard )
527+ self ._keyboard_layout = self . _layout_class (self .keyboard )
505528 return self ._keyboard_layout
506529
507530 @property
0 commit comments