def _sync(self): events = [] for ev in self._events_state.keys(): if self._events_state[ev] is not None: events.append(InputEvent(evd.evbit(ev), self._events_state[ev])) events.append(InputEvent(evd.EV_SYN.SYN_REPORT, 0)) self.uinput_node.send_events(events)
def deactivate_numlock(): events = [ InputEvent(EV_KEY.KEY_NUMLOCK, 0), InputEvent(EV_SYN.SYN_REPORT, 0) ] udev.send_events(events) d_t.ungrab() subprocess.call(offCmd, shell=True)
def deactivate_numlock(): numpad_cmd = "i2ctransfer -f -y " + device_id + " w13@0x15 0x05 0x00 0x3d 0x03 0x06 0x00 0x07 0x00 0x0d 0x14 0x03 0x00 0xad" events = [ InputEvent(EV_KEY.KEY_NUMLOCK, 0), InputEvent(EV_SYN.SYN_REPORT, 0) ] udev.send_events(events) d_t.ungrab() subprocess.call(numpad_cmd, shell=True)
def activate_numlock(brightness): numpad_cmd = "i2ctransfer -f -y " + device_id + " w13@0x15 0x05 0x00 0x3d 0x03 0x06 0x00 0x07 0x00 0x0d 0x14 0x03 " + BRIGHT_VAL[ brightness] + " 0xad" events = [ InputEvent(EV_KEY.KEY_NUMLOCK, 1), InputEvent(EV_SYN.SYN_REPORT, 0) ] udev.send_events(events) d_t.grab() subprocess.call(numpad_cmd, shell=True)
def __key2seq(self, key, downup=1): """ 构建事件序列。 """ if downup == 1 or downup == 0: pass else: raise ValueError("param: downup chioce 1 or 0.") shiftdown = [ InputEvent(ev.EV_MSC.MSC_SCAN, value=4), InputEvent(evbit("KEY_LEFTSHIFT"), value=1), InputEvent(ev.EV_SYN.SYN_REPORT, value=0) ] shiftup = [ InputEvent(ev.EV_MSC.MSC_SCAN, value=4), InputEvent(evbit("KEY_LEFTSHIFT"), value=0), InputEvent(ev.EV_SYN.SYN_REPORT, value=0) ] key_seq = [ InputEvent(ev.EV_MSC.MSC_SCAN, value=4), InputEvent(evbit("KEY_" + key.upper()), value=downup), InputEvent(ev.EV_SYN.SYN_REPORT, value=0) ] if key.isupper(): event_seq = shiftdown + key_seq + shiftup elif key.islower(): event_seq = key_seq elif key.isdigit(): event_seq = key_seq return event_seq
def __mousebtn2seq(self, btn, downup=1): """ 鼠标点击。 param: btn: LEFT, RIGHT, MIDDLE param: downup: 1 or 0 """ btn = btn.upper() if btn == "LEFT": event_seq = [ InputEvent(ev.EV_MSC.MSC_SCAN, value=4), InputEvent(ev.EV_KEY.BTN_LEFT, value=downup), InputEvent(ev.EV_SYN.SYN_REPORT, value=0) ] elif btn == "RIGHT": event_seq = [ InputEvent(ev.EV_MSC.MSC_SCAN, value=4), InputEvent(ev.EV_KEY.BTN_RIGHT, value=downup), InputEvent(ev.EV_SYN.SYN_REPORT, value=0) ] elif btn == "MIDDLE": event_seq = [ InputEvent(ev.EV_MSC.MSC_SCAN, value=4), InputEvent(ev.EV_KEY.BTN_MIDDLE, value=downup), InputEvent(ev.EV_SYN.SYN_REPORT, value=0) ] else: raise ValueError("btn require is LEFT or RIGHT or MIDDLE") return event_seq
def launch_calculator(): try: events = [ InputEvent(calculator_key, 1), InputEvent(EV_SYN.SYN_REPORT, 0), InputEvent(calculator_key, 0), InputEvent(EV_SYN.SYN_REPORT, 0) ] udev.send_events(events) except OSError as e: pass
def _macro(self, event): if event.code not in self._macros: logger.debug(f'Unbound key {event.code.name}') return macro = self._macros[event.code] for k, v in macro: event = InputEvent(k, value=v) syn = InputEvent(libevdev.EV_SYN.SYN_REPORT, value=0) self._uinput.send_events([event, syn]) time.sleep(0.008) logger.debug(f'Macros sent: {" ".join([x[0].name for x in macro])}')
def __mousemove2seq(self, rel_x=0, rel_y=0): """ param: (rel_x, rel_y) """ if isinstance(rel_x, int) and isinstance(rel_y, int): pass else: raise ValueError("rel_x rel_y require is int") event_seq = [ InputEvent(ev.EV_REL.REL_X, value=rel_x), InputEvent(ev.EV_REL.REL_Y, value=rel_y), InputEvent(ev.EV_SYN.SYN_REPORT, value=0) ] return event_seq
def main(args): dev = libevdev.Device() dev.name = "test device" dev.enable(libevdev.EV_REL.REL_X) dev.enable(libevdev.EV_REL.REL_Y) try: dev.create() print("New device at {} ({})".format(dev.devnode, dev.syspath)) events = [ InputEvent(libevdev.EV_REL.REL_X, -1), InputEvent(libevdev.EV_REL.REL_Y, 1), InputEvent(libevdev.EV_SYN.SYN_REPORT, 0) ] dev.send_events(events) except OSError as e: print(e)
def filter_chatter(event: InputEvent) -> Union[InputEvent, None]: """Filter input events that are coming too fast""" # no need to relay those - we are going to emit our own if event.matches(libevdev.EV_SYN) or event.matches(libevdev.EV_MSC): logging.debug(f'Got {event.code} - filtering') return None # some events we don't want to filter, like EV_LED for toggling NumLock and the like, and also key hold events if not event.matches(libevdev.EV_KEY) or event.value > 1: logging.debug(f'Got {event.code} - letting through') return event # the values are 0 for up, 1 for down and 2 for hold if event.value == 0 and not key_pressed[event.code]: logging.debug( f'Got {event.code} up, but key is not pressed - filtering') return None if event.value == 0 and key_pressed[event.code]: logging.debug(f'Got {event.code} up - letting through') last_key_up[event.code] = event.sec * 1E6 + event.usec key_pressed[event.code] = False return event prev = last_key_up.get(event.code) now = event.sec * 1E6 + event.usec if prev is None or now - prev > threshold * 1E3: logging.debug(f'Got {event.code} down - letting through') key_pressed[event.code] = True return event logging.debug( f'Got {event.code} down, but last key up was just {(now - prev) / 1E3} ms ago - filtering' ) return None
def wrapModifierKeys(list, ctrl, shift, alt): if shift: list.insert(0, InputEvent(libevdev.EV_KEY.KEY_LEFTSHIFT,1)) list.append(InputEvent(libevdev.EV_KEY.KEY_LEFTSHIFT,0)) if alt: list.insert(0, InputEvent(libevdev.EV_KEY.KEY_RIGHTALT,1)) list.append(InputEvent(libevdev.EV_KEY.KEY_RIGHTALT,0)) if ctrl: list.insert(0, InputEvent(libevdev.EV_KEY.KEY_LEFTCTRL,1)) list.append(InputEvent(libevdev.EV_KEY.KEY_LEFTCTRL,0)) return list
def __mousewheel2seq(self, updown=1): """ EV_REL.REL_0B value 与鼠标滚轮方向对应为 -120 or 120 param: updown: 鼠标滚轮方向,value=1:向上,value=-1 向下 """ if updown == 1: event_seq = [ InputEvent(ev.EV_REL.REL_WHEEL, value=1), InputEvent(ev.EV_REL.REL_0B, value=120), InputEvent(ev.EV_SYN.SYN_REPORT, value=0) ] elif updown == -1: event_seq = [ InputEvent(ev.EV_REL.REL_WHEEL, value=-1), InputEvent(ev.EV_REL.REL_0B, value=-120), InputEvent(ev.EV_SYN.SYN_REPORT, value=0) ] else: raise ValueError("updown chioce: 1 or -1.") return event_seq
def deadlessIt(key, ctrl, shift): list = wrapModifierKeys([InputEvent(key, 1)], ctrl, shift, False) list.append(InputEvent(key,0)) list.append(InputEvent(libevdev.EV_SYN.SYN_REPORT,0)) return list
x = e.value continue # Get y position # if e.matches(EV_ABS.ABS_MT_POSITION_Y): y = e.value continue # If tap # if e.matches(EV_KEY.BTN_TOOL_FINGER): # If end of tap, send release key event # if e.value == 0: finger = 0 try: if value: events = [ InputEvent(EV_KEY.KEY_LEFTSHIFT, 0), InputEvent(value, 0), InputEvent(EV_SYN.SYN_REPORT, 0) ] udev.send_events(events) value = None pass except OSError as e: pass # Start of tap # if finger == 0 and e.value == 1: finger = 1 # Check if numlock was hit # if (e.matches(EV_KEY.BTN_TOOL_FINGER) and e.value == 1
x = e.value continue # Get y position # if e.matches(EV_ABS.ABS_MT_POSITION_Y): y = e.value continue # If tap # if e.matches(EV_KEY.BTN_TOOL_FINGER): # If end of tap, send release key event # if e.value == 0: finger = 0 try: if value: events = [ InputEvent(value, 0), InputEvent(EV_SYN.SYN_REPORT, 0) ] udev.send_events(events) value = None pass except OSError as e: pass # Start of tap # if finger == 0 and e.value == 1: finger = 1 # Check if numlock was hit # if ( e.matches(EV_KEY.BTN_TOOL_FINGER) and
def deadCapsIt(e, shift, alt): list = wrapModifierKeys([e], False, shift, alt) list.append(InputEvent(libevdev.EV_SYN.SYN_REPORT,0)) return list
# Get y position # if e.matches(EV_ABS.ABS_MT_POSITION_Y): y = e.value continue # Else event is tap: e.matches(EV_KEY.BTN_TOOL_FINGER) # # If end of tap, send release key event # if e.value == 0: log.debug('finger up at x %d y %d', x, y) if button_pressed: log.debug('send key up event %s', button_pressed) events = [ InputEvent(EV_KEY.KEY_LEFTSHIFT, 0), InputEvent(button_pressed, 0), InputEvent(EV_SYN.SYN_REPORT, 0) ] try: udev.send_events(events) button_pressed = None except OSError as err: log.error("Cannot send release event, %s", err) pass elif e.value == 1 and not button_pressed: # Start of tap # log.debug('finger down at x %d y %d', x, y) # Check if numlock was hit #
def send_event_packet(device: Device, event: InputEvent): """Send an event packet properly ending with a SYN_REPORT.""" packet = [event, InputEvent(libevdev.EV_SYN.SYN_REPORT, 0)] device.send_events(packet)