#!/usr/bin/env python3 import evdev import argparse import os import sys scan_to_hid = { evdev.ecodes.KEY_A: 0x04, evdev.ecodes.KEY_B: 0x05, evdev.ecodes.KEY_C: 0x06, evdev.ecodes.KEY_D: 0x07, evdev.ecodes.KEY_E: 0x08, evdev.ecodes.KEY_F: 0x09, evdev.ecodes.KEY_G: 0x0a, evdev.ecodes.KEY_H: 0x0b, evdev.ecodes.KEY_I: 0x0c, evdev.ecodes.KEY_J: 0x0d, evdev.ecodes.KEY_K: 0x0e, evdev.ecodes.KEY_L: 0x0f, evdev.ecodes.KEY_M: 0x10, evdev.ecodes.KEY_N: 0x11, evdev.ecodes.KEY_O: 0x12, evdev.ecodes.KEY_P: 0x13, evdev.ecodes.KEY_Q: 0x14, evdev.ecodes.KEY_R: 0x16, evdev.ecodes.KEY_S: 0x15, evdev.ecodes.KEY_T: 0x17, evdev.ecodes.KEY_U: 0x18, evdev.ecodes.KEY_V: 0x19, evdev.ecodes.KEY_W: 0x1a, evdev.ecodes.KEY_X: 0x1b, evdev.ecodes.KEY_Y: 0x1c, evdev.ecodes.KEY_Z: 0x1d, } LEFT_CTRL = 1 << 0 LEFT_SHIFT = 1 << 1 LEFT_ALT = 1 << 2 LEFT_GUI = 1 << 3 RIGHT_CTRL = 1 << 4 RIGHT_SHIFT = 1 << 5 RIGHT_ALT = 1 << 6 RIGHT_GUI = 1 << 7 if __name__ == '__main__': parser = argparse.ArgumentParser(description='Forward input events to a USB descriptor') parser.add_argument('input', type=str, help='The source evdev device') parser.add_argument('output', type=str, help='The output HID device') args = parser.parse_args() dst = os.open(args.output, os.O_WRONLY) src = evdev.InputDevice(args.input) src.grab() modifiers = 0 keys_down = set() for event in src.read_loop(): if event.type != evdev.ecodes.EV_KEY: continue data = evdev.categorize(event) print("Got key", data.scancode) code = scan_to_hid.get(data.scancode, None) if code is None: print("Ignoring unknown key") continue if data.keystate == data.key_down: if len(keys_down) >= 6: print("Ignoring key due to rollover") continue print("Adding key") keys_down.add(code) if data.keystate == data.key_up: print("Removing key") keys_down.remove(code) # Build the packet packet = [0] + [0] + [k for k in keys_down] packet += [0] * (8 - len(packet)) assert(len(packet) == 8) print("Sending", packet) os.write(dst, bytes(packet))