diff --git a/code.py b/code.py index 3f468f0..b6b69cb 100644 --- a/code.py +++ b/code.py @@ -138,4 +138,9 @@ def pairs_handler(dev, n): # Pairs: J & K, U & I keyboard.pairs = [{35, 36}, {20, 19}] +# Set a default light color and enable light on keypress +keyboard.set_default_color(r=0xFF, g=0, b=0xFF) +keyboard.light_on_press = True + + keyboard.run() diff --git a/keyboard/__init__.py b/keyboard/__init__.py index f234ef0..1faea6a 100644 --- a/keyboard/__init__.py +++ b/keyboard/__init__.py @@ -101,6 +101,13 @@ def __init__(self, keymap=(), pairs=(), verbose=True): self.tap_delay = 500 self.fast_type_thresh = 200 self.pair_delay = 10 + self.default_color = [0xFF, 0xFF, 0xFF] + self.light_on_press = False + self.RGB_OFF = 0 + self.RGB_TAP = 1 + self.RGB_FILL = 2 + self.RGB_EMPTY = 3 + self.rgb_mode = self.RGB_OFF self._connection = "" @@ -124,6 +131,9 @@ def __init__(self, keymap=(), pairs=(), verbose=True): self.ble_hid = HID(ble_hid.devices) self.usb_hid = HID(usb_hid.devices) + def set_default_color(self, r=0xFF, g=0xFF, b=0xFF): + self.default_color = [r, g, b] + def update_connection(self): if usb_is_connected() and self.usb_status == 3: conn = "USB" @@ -398,10 +408,29 @@ def send_consumer(self, keycode): except Exception as e: print(e) + def update_rgb(self, event, pressed): + if self.rgb_mode == self.RGB_EMPTY and not self.backlight.dev.any(): + self.backlight.on(*self.default_color) + return + key = event if pressed else (event & 0x7F) + if self.rgb_mode == self.RGB_EMPTY: + color = [0, 0, 0] + else: + color = self.default_color if pressed else [0, 0, 0] + if self.rgb_mode == self.RGB_TAP or ((self.rgb_mode == self.RGB_FILL or self.rgb_mode == self.RGB_EMPTY) and pressed): + self.backlight.pixel(key, *color) + if key == 56: + self.backlight.pixel(61, *color) + self.backlight.pixel(62, *color) + self.backlight.update() + + def get(self): key = self.matrix.get() - if key & 0x80 == 0: + pressed = key & 0x80 == 0 + if pressed: self.heatmap[key] += 1 + self.update_rgb(key, pressed) return key def run(self):