def change_page(ui, page: int) -> None: api.set_page(_deck_id(ui), page) redraw_buttons(ui) _highlight_first_button(ui)
def handle_keypress(deck_id: str, key: int, state: bool) -> None: if state: if dimmers[deck_id].reset(): return keyboard = Controller() page = api.get_page(deck_id) command = api.get_button_command(deck_id, page, key) if command: try: Popen(shlex.split(command)) except Exception as error: print(f"The command '{command}' failed: {error}") keys = api.get_button_keys(deck_id, page, key) if keys: keys = keys.strip().replace(" ", "") for section in keys.split(","): # Since + and , are used to delimit our section and keys to press, # they need to be substituded with keywords. section_keys = [_replace_special_keys(key_name) for key_name in section.split("+")] # Translate string to enum, or just the string itself if not found section_keys = [getattr(Key, key_name.lower(), key_name) for key_name in section_keys] for key_name in section_keys: try: if key_name == "delay": time.sleep(0.5) else: keyboard.press(key_name) except Exception: print(f"Could not press key '{key_name}'") for key_name in section_keys: try: if key_name != "delay": keyboard.release(key_name) except Exception: print(f"Could not release key '{key_name}'") write = api.get_button_write(deck_id, page, key) if write: try: keyboard.type(write) except Exception as error: print(f"Could not complete the write command: {error}") brightness_change = api.get_button_change_brightness(deck_id, page, key) if brightness_change: try: api.change_brightness(deck_id, brightness_change) dimmers[deck_id].brightness = api.get_brightness(deck_id) dimmers[deck_id].reset() except Exception as error: print(f"Could not change brightness: {error}") switch_page = api.get_button_switch_page(deck_id, page, key) if switch_page: api.set_page(deck_id, switch_page - 1)
def change_page(ui, page: int) -> None: deck_id = _deck_id(ui) api.set_page(deck_id, page) redraw_buttons(ui) _highlight_first_button(ui) dimmers[deck_id].reset()
def handle_keypress(deck_id: str, key: int, state: bool) -> None: if state: if dimmers[deck_id].reset(): return keyboard = Controller() page = api.get_page(deck_id) command = api.get_button_command(deck_id, page, key) if command: try: Popen(shlex.split(command)) except Exception as error: print(f"The command '{command}' failed: {error}") keys = api.get_button_keys(deck_id, page, key) if keys: keys = keys.strip().replace(" ", "") for section in keys.split(","): # Since + and , are used to delimit our section and keys to press, # they need to be substituted with keywords. section_keys = [ _replace_special_keys(key_name) for key_name in section.split("+") ] # Translate string to enum, or just the string itself if not found section_keys = [ getattr(Key, key_name.lower(), key_name) for key_name in section_keys ] for key_name in section_keys: if isinstance(key_name, str) and key_name.startswith("delay"): sleep_time_arg = key_name.split("delay", 1)[1] if sleep_time_arg: try: sleep_time = float(sleep_time_arg) except Exception: print( f"Could not convert sleep time to float '{sleep_time_arg}'" ) sleep_time = 0 else: # default if not specified sleep_time = 0.5 if sleep_time: try: time.sleep(sleep_time) except Exception: print( f"Could not sleep with provided sleep time '{sleep_time}'" ) else: try: keyboard.press(key_name) except Exception: print(f"Could not press key '{key_name}'") for key_name in section_keys: if not (isinstance(key_name, str) and key_name.startswith("delay")): try: keyboard.release(key_name) except Exception: print(f"Could not release key '{key_name}'") write = api.get_button_write(deck_id, page, key) if write: try: keyboard.type(write) except Exception as error: print(f"Could not complete the write command: {error}") brightness_change = api.get_button_change_brightness( deck_id, page, key) if brightness_change: try: api.change_brightness(deck_id, brightness_change) dimmers[deck_id].brightness = api.get_brightness(deck_id) dimmers[deck_id].reset() except Exception as error: print(f"Could not change brightness: {error}") switch_page = api.get_button_switch_page(deck_id, page, key) target_device = api.get_target_device(deck_id, page, key) if switch_page: api.set_page(target_device, switch_page - 1)