class Output: def __init__(self): try: self.uinput = UInput() except UInputError: print("Error when opening /dev/uinput") print("Ensure you have write permissions") exit(1) def sync(self): self.uinput.syn() def send(self, key, action): # IMPORTANT - choose Right Alt over Left Alt self.uinput.write(ecodes.EV_KEY, key.codes[-1], int(action)) self.sync() def __del__(self): try: self.uinput.close() except ImportError: pass
def create_ui(on_write): ui = UInput() ui.write = Mock(side_effect=on_write) return ui
def send_event(event): _uinput.write_event(event) send_sync() def send_key_action(key, action, hooks=None): update_modifier_key_pressed(key, action) original_pressed_keys = _pressed_keys.copy() update_pressed_keys(key, action) if hooks: if action == action.RELEASE and \ key in original_pressed_keys and \ key not in _pressed_keys and \ callable(hook := hooks.get(key, None)): hook() _uinput.write(ecodes.EV_KEY, key, action) send_sync() def send_combo(combo, hooks=None): released_modifiers_keys = [] extra_modifier_keys = _pressed_modifier_keys.copy() missing_modifiers = combo.modifiers.copy() for pressed_key in _pressed_modifier_keys: for modifier in combo.modifiers: if pressed_key in modifier.get_keys(): extra_modifier_keys.remove(pressed_key) missing_modifiers.remove(modifier)