# https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h
keys = {
    0: "RESERVED",
    1: "ESC",
    2: "1",
    3: "2",
    4: "3",
    5: "4",
    6: "5",
    7: "6",
    8: "7",
    9: "8",
    10: "9",
    11: "0",
    12: "-",
    13: "=",
    14: "BACKSPACE",
    15: "TAB",
    16: "Q",
    17: "W",
    18: "E",
    19: "R",
    20: "T",
    21: "Y",
    22: "U",
    23: "I",
    24: "O",
    25: "P",
    26: "{",
    27: "}",
    28: "ENTER",
    29: "CTRL",  # LEFT
    30: "A",
    31: "S",
    32: "D",
    33: "F",
    34: "G",
    35: "H",
    36: "J",
    37: "K",
    38: "L",
    39: ";",
    40: "'",
    41: "`",
    42: "SHIFT",  # LEFT
    43: "\\",
    44: "Z",
    45: "X",
    46: "C",
    47: "V",
    48: "B",
    49: "N",
    50: "M",
    51: ",",
    52: ".",
    53: "/",
    54: "SHIFT",  # RIGHT
    55: "*",
    56: "ALT",  # LEFT
    57: "SPACE",
    58: "CAPSLOCK",
    59: "F1",
    60: "F2",
    61: "F3",
    62: "F4",
    63: "F5",
    64: "F6",
    65: "F7",
    66: "F8",
    67: "F9",
    68: "F10",
    # Numpad
    69: "NUMLOCK",
    70: "SCROLLLOCK",
    71: "7",
    72: "8",
    73: "9",
    74: "-",
    75: "4",
    76: "5",
    77: "6",
    78: "+",
    79: "1",
    80: "2",
    81: "3",
    82: "0",
    83: ".",
    85: "ZENKAKUHANKAKU",
    86: "102ND",
    87: "F11",
    88: "F12",
    89: "RO",
    90: "KATAKANA",
    91: "HIRAGANA",
    92: "HENKAN",
    93: "KATAKANAHIRAGANA",
    94: "MUHENKAN",
    95: "KPJPCOMMA",
    96: "KPENTER",
    97: "CTRL",  # RIGHT
    98: "KPSLASH",
    99: "SYSRQ",
    100: "ALT",  # RIGHT
    101: "LINEFEED",
    102: "HOME",
    103: "UP",
    104: "PAGEUP",
    105: "LEFT",
    106: "RIGHT",
    107: "END",
    108: "DOWN",
    109: "PAGEDOWN",
    110: "INSERT",
    111: "DELETE",
    112: "MACRO",
    113: "MUTE",
    114: "VOLUMEDOWN",
    115: "VOLUMEUP",
    116: "POWER",
    117: "KPEQUAL",
    118: "KPPLUSMINUS",
    119: "PAUSE",
    120: "SCALE",
    121: "KPCOMMA",
    122: "HANGEUL",
    123: "HANJA",
    124: "YEN",
    125: "META",  # LEFT
    126: "META",  # RIGHT
    127: "COMPOSE",
    128: "STOP",
    129: "AGAIN",
    130: "PROPS",
    131: "UNDO",
    132: "FRONT",
    133: "COPY",
    134: "OPEN",
    135: "PASTE",
    136: "FIND",
    137: "CUT",
    138: "HELP",
    139: "MENU",
    140: "CALC",
    141: "SETUP",
    142: "SLEEP",
    143: "WAKEUP",
    144: "FILE",
    145: "SENDFILE",
    146: "DELETEFILE",
    147: "XFER",
    148: "PROG1",
    149: "PROG2",
    150: "WWW",
    151: "MSDOS",
    152: "COFFEE",
    153: "ROTATE_DISPLAY",
    154: "CYCLEWINDOWS",
    155: "MAIL",
    156: "BOOKMARKS",
    157: "COMPUTER",
    158: "BACK",
    159: "FORWARD",
    160: "CLOSECD",
    161: "EJECTCD",
    162: "EJECTCLOSECD",
    163: "NEXTSONG",
    164: "PLAYPAUSE",
    165: "PREVIOUSSONG",
    166: "STOPCD",
    167: "RECORD",
    168: "REWIND",
    169: "PHONE",
    170: "ISO",
    171: "CONFIG",
    172: "HOMEPAGE",
    173: "REFRESH",
    174: "EXIT",
    175: "MOVE",
    176: "EDIT",
    177: "SCROLLUP",
    178: "SCROLLDOWN",
    179: "(",
    180: ")",
    181: "NEW",
    182: "REDO",
    183: "F13",
    184: "F14",
    185: "F15",
    186: "F16",
    187: "F17",
    188: "F18",
    189: "F19",
    190: "F20",
    191: "F21",
    192: "F22",
    193: "F23",
    194: "F24",
    200: "PLAYCD",
    201: "PAUSECD",
    202: "PROG3",
    203: "PROG4",
    204: "DASHBOARD",
    205: "SUSPEND",
    206: "CLOSE",
    207: "PLAY",
    208: "FASTFORWARD",
    209: "BASSBOOST",
    210: "PRINT",
    211: "HP",
    212: "CAMERA",
    213: "SOUND",
    214: "QUESTION",
    215: "EMAIL",
    216: "CHAT",
    217: "SEARCH",
    218: "CONNECT",
    219: "FINANCE",
    220: "SPORT",
    221: "SHOP",
    222: "ALTERASE",
    223: "CANCEL",
    224: "BRIGHTNESSDOWN",
    225: "BRIGHTNESSUP",
    226: "MEDIA",
    227: "SWITCHVIDEOMODE",
    228: "KBDILLUMTOGGLE",
    229: "KBDILLUMDOWN",
    230: "KBDILLUMUP",
    231: "SEND",
    232: "REPLY",
    233: "FORWARDMAIL",
    234: "SAVE",
    235: "DOCUMENTS",
    236: "BATTERY",
    237: "BLUETOOTH",
    238: "WLAN",
    239: "UWB",
    240: "UNKNOWN",
    241: "VIDEO_NEXT",
    242: "VIDEO_PREV",
    243: "BRIGHTNESS_CYCLE",
    244: "BRIGHTNESS_AUTO",
    245: "DISPLAY_OFF",
    246: "WWAN",
    247: "RFKILL",
    248: "MICMUTE",
    0x100: "MISC_0",
    0x101: "MISC_1",
    0x102: "MISC_2",
    0x103: "MISC_3",
    0x104: "MISC_4",
    0x105: "MISC_5",
    0x106: "MISC_6",
    0x107: "MISC_7",
    0x108: "MISC_8",
    0x109: "MISC_9",
    # 0x110: 'MOUSE_LEFT',
    # 0x111: 'MOUSE_RIGHT',
    # 0x112: 'MOUSE_MIDDLE',
    # 0x113: 'MOUSE_SIDE',
    # 0x114: 'MOUSE_EXTRA',
    # 0x115: 'MOUSE_FORWARD',
    # 0x116: 'MOUSE_BACK',
    # 0x117: 'MOUSE_TASK',
    # 0x120: 'JOYSTICK_TRIGGER',
    # 0x121: 'JOYSTICK_THUMB',
    # 0x122: 'JOYSTICK_THUMB2',
    # 0x123: 'JOYSTICK_TOP',
    # 0x124: 'JOYSTICK_TOP2',
    # 0x125: 'JOYSTICK_PINKIE',
    # 0x126: 'JOYSTICK_BASE',
    # 0x127: 'JOYSTICK_BASE2',
    # 0x128: 'JOYSTICK_BASE3',
    # 0x129: 'JOYSTICK_BASE4',
    # 0x12a: 'JOYSTICK_BASE5',
    # 0x12b: 'JOYSTICK_BASE6',
    # 0x12f: 'JOYSTICK_DEAD',
    #
    # 0x130: 'GAMEPAD_SOUTH',
    # 0x131: 'GAMEPAD_EAST',
    # 0x132: 'GAMEPAD_C',
    # 0x133: 'GAMEPAD_NORTH',
    # 0x134: 'GAMEPAD_WEST',
    # 0x135: 'GAMEPAD_Z',
    # 0x136: 'GAMEPAD_TL',
    # 0x137: 'GAMEPAD_TR',
    # 0x138: 'GAMEPAD_TL2',
    # 0x139: 'GAMEPAD_TR2',
    # 0x13a: 'GAMEPAD_SELECT',
    # 0x13b: 'GAMEPAD_START',
    # 0x13c: 'GAMEPAD_MODE',
    # 0x13d: 'GAMEPAD_THUMBL',
    # 0x13e: 'GAMEPAD_THUMBR',
    #
    # 0x140: 'DIGI_TOOL_PEN',
    # 0x141: 'DIGI_TOOL_RUBBER',
    # 0x142: 'DIGI_TOOL_BRUSH',
    # 0x143: 'DIGI_TOOL_PENCIL',
    # 0x144: 'DIGI_TOOL_AIRBRUSH',
    # 0x145: 'DIGI_TOOL_FINGER',
    # 0x146: 'DIGI_TOOL_MOUSE',
    # 0x147: 'DIGI_TOOL_LENS',
    # 0x148: 'DIGI_TOOL_QUINTTAP',
    # 0x149: 'DIGI_STYLUS3',
    # 0x14a: 'DIGI_TOUCH',
    # 0x14b: 'DIGI_STYLUS',
    # 0x14c: 'DIGI_STYLUS2',
    # 0x14d: 'DIGI_TOOL_DOUBLETAP',
    # 0x14e: 'DIGI_TOOL_TRIPLETAP',
    # 0x14f: 'DIGI_TOOL_QUADTAP',
    # 0x150: 'WHEEL_GEAR_DOWN',
    # 0x151: 'WHEEL_GEAR_UP',
}


def translate_keycode(code, ctrl=0, shift=0, alt=0, meta=0):
    try:
        key = keys[code]
        key = (
            f"{'CTRL+' if ctrl else ''}{'ALT+' if alt else ''}{'SHIFT+' if shift else ''}"
            f"{'META+' if meta else ''}{key}"
        )
        return key
    except KeyError:
        return None

# https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h
keys = {
    0: "RESERVED",
    1: "ESC",
    2: "1",
    3: "2",
    4: "3",
    5: "4",
    6: "5",
    7: "6",
    8: "7",
    9: "8",
    10: "9",
    11: "0",
    12: "-",
    13: "=",
    14: "BACKSPACE",
    15: "TAB",
    16: "Q",
    17: "W",
    18: "E",
    19: "R",
    20: "T",
    21: "Y",
    22: "U",
    23: "I",
    24: "O",
    25: "P",
    26: "{",
    27: "}",
    28: "ENTER",
    29: "CTRL",  # LEFT
    30: "A",
    31: "S",
    32: "D",
    33: "F",
    34: "G",
    35: "H",
    36: "J",
    37: "K",
    38: "L",
    39: ";",
    40: "'",
    41: "`",
    42: "SHIFT",  # LEFT
    43: "\\",
    44: "Z",
    45: "X",
    46: "C",
    47: "V",
    48: "B",
    49: "N",
    50: "M",
    51: ",",
    52: ".",
    53: "/",
    54: "SHIFT",  # RIGHT
    55: "*",
    56: "ALT",  # LEFT
    57: "SPACE",
    58: "CAPSLOCK",
    59: "F1",
    60: "F2",
    61: "F3",
    62: "F4",
    63: "F5",
    64: "F6",
    65: "F7",
    66: "F8",
    67: "F9",
    68: "F10",
    # Numpad
    69: "NUMLOCK",
    70: "SCROLLLOCK",
    71: "7",
    72: "8",
    73: "9",
    74: "-",
    75: "4",
    76: "5",
    77: "6",
    78: "+",
    79: "1",
    80: "2",
    81: "3",
    82: "0",
    83: ".",
    85: "ZENKAKUHANKAKU",
    86: "102ND",
    87: "F11",
    88: "F12",
    89: "RO",
    90: "KATAKANA",
    91: "HIRAGANA",
    92: "HENKAN",
    93: "KATAKANAHIRAGANA",
    94: "MUHENKAN",
    95: "KPJPCOMMA",
    96: "KPENTER",
    97: "CTRL",  # RIGHT
    98: "KPSLASH",
    99: "SYSRQ",
    100: "ALT",  # RIGHT
    101: "LINEFEED",
    102: "HOME",
    103: "UP",
    104: "PAGEUP",
    105: "LEFT",
    106: "RIGHT",
    107: "END",
    108: "DOWN",
    109: "PAGEDOWN",
    110: "INSERT",
    111: "DELETE",
    112: "MACRO",
    113: "MUTE",
    114: "VOLUMEDOWN",
    115: "VOLUMEUP",
    116: "POWER",
    117: "KPEQUAL",
    118: "KPPLUSMINUS",
    119: "PAUSE",
    120: "SCALE",
    121: "KPCOMMA",
    122: "HANGEUL",
    123: "HANJA",
    124: "YEN",
    125: "META",  # LEFT
    126: "META",  # RIGHT
    127: "COMPOSE",
    128: "STOP",
    129: "AGAIN",
    130: "PROPS",
    131: "UNDO",
    132: "FRONT",
    133: "COPY",
    134: "OPEN",
    135: "PASTE",
    136: "FIND",
    137: "CUT",
    138: "HELP",
    139: "MENU",
    140: "CALC",
    141: "SETUP",
    142: "SLEEP",
    143: "WAKEUP",
    144: "FILE",
    145: "SENDFILE",
    146: "DELETEFILE",
    147: "XFER",
    148: "PROG1",
    149: "PROG2",
    150: "WWW",
    151: "MSDOS",
    152: "COFFEE",
    153: "ROTATE_DISPLAY",
    154: "CYCLEWINDOWS",
    155: "MAIL",
    156: "BOOKMARKS",
    157: "COMPUTER",
    158: "BACK",
    159: "FORWARD",
    160: "CLOSECD",
    161: "EJECTCD",
    162: "EJECTCLOSECD",
    163: "NEXTSONG",
    164: "PLAYPAUSE",
    165: "PREVIOUSSONG",
    166: "STOPCD",
    167: "RECORD",
    168: "REWIND",
    169: "PHONE",
    170: "ISO",
    171: "CONFIG",
    172: "HOMEPAGE",
    173: "REFRESH",
    174: "EXIT",
    175: "MOVE",
    176: "EDIT",
    177: "SCROLLUP",
    178: "SCROLLDOWN",
    179: "(",
    180: ")",
    181: "NEW",
    182: "REDO",
    183: "F13",
    184: "F14",
    185: "F15",
    186: "F16",
    187: "F17",
    188: "F18",
    189: "F19",
    190: "F20",
    191: "F21",
    192: "F22",
    193: "F23",
    194: "F24",
    200: "PLAYCD",
    201: "PAUSECD",
    202: "PROG3",
    203: "PROG4",
    204: "DASHBOARD",
    205: "SUSPEND",
    206: "CLOSE",
    207: "PLAY",
    208: "FASTFORWARD",
    209: "BASSBOOST",
    210: "PRINT",
    211: "HP",
    212: "CAMERA",
    213: "SOUND",
    214: "QUESTION",
    215: "EMAIL",
    216: "CHAT",
    217: "SEARCH",
    218: "CONNECT",
    219: "FINANCE",
    220: "SPORT",
    221: "SHOP",
    222: "ALTERASE",
    223: "CANCEL",
    224: "BRIGHTNESSDOWN",
    225: "BRIGHTNESSUP",
    226: "MEDIA",
    227: "SWITCHVIDEOMODE",
    228: "KBDILLUMTOGGLE",
    229: "KBDILLUMDOWN",
    230: "KBDILLUMUP",
    231: "SEND",
    232: "REPLY",
    233: "FORWARDMAIL",
    234: "SAVE",
    235: "DOCUMENTS",
    236: "BATTERY",
    237: "BLUETOOTH",
    238: "WLAN",
    239: "UWB",
    240: "UNKNOWN",
    241: "VIDEO_NEXT",
    242: "VIDEO_PREV",
    243: "BRIGHTNESS_CYCLE",
    244: "BRIGHTNESS_AUTO",
    245: "DISPLAY_OFF",
    246: "WWAN",
    247: "RFKILL",
    248: "MICMUTE",
    0x100: "MISC_0",
    0x101: "MISC_1",
    0x102: "MISC_2",
    0x103: "MISC_3",
    0x104: "MISC_4",
    0x105: "MISC_5",
    0x106: "MISC_6",
    0x107: "MISC_7",
    0x108: "MISC_8",
    0x109: "MISC_9",
    # 0x110: 'MOUSE_LEFT',
    # 0x111: 'MOUSE_RIGHT',
    # 0x112: 'MOUSE_MIDDLE',
    # 0x113: 'MOUSE_SIDE',
    # 0x114: 'MOUSE_EXTRA',
    # 0x115: 'MOUSE_FORWARD',
    # 0x116: 'MOUSE_BACK',
    # 0x117: 'MOUSE_TASK',
    # 0x120: 'JOYSTICK_TRIGGER',
    # 0x121: 'JOYSTICK_THUMB',
    # 0x122: 'JOYSTICK_THUMB2',
    # 0x123: 'JOYSTICK_TOP',
    # 0x124: 'JOYSTICK_TOP2',
    # 0x125: 'JOYSTICK_PINKIE',
    # 0x126: 'JOYSTICK_BASE',
    # 0x127: 'JOYSTICK_BASE2',
    # 0x128: 'JOYSTICK_BASE3',
    # 0x129: 'JOYSTICK_BASE4',
    # 0x12a: 'JOYSTICK_BASE5',
    # 0x12b: 'JOYSTICK_BASE6',
    # 0x12f: 'JOYSTICK_DEAD',
    #
    # 0x130: 'GAMEPAD_SOUTH',
    # 0x131: 'GAMEPAD_EAST',
    # 0x132: 'GAMEPAD_C',
    # 0x133: 'GAMEPAD_NORTH',
    # 0x134: 'GAMEPAD_WEST',
    # 0x135: 'GAMEPAD_Z',
    # 0x136: 'GAMEPAD_TL',
    # 0x137: 'GAMEPAD_TR',
    # 0x138: 'GAMEPAD_TL2',
    # 0x139: 'GAMEPAD_TR2',
    # 0x13a: 'GAMEPAD_SELECT',
    # 0x13b: 'GAMEPAD_START',
    # 0x13c: 'GAMEPAD_MODE',
    # 0x13d: 'GAMEPAD_THUMBL',
    # 0x13e: 'GAMEPAD_THUMBR',
    #
    # 0x140: 'DIGI_TOOL_PEN',
    # 0x141: 'DIGI_TOOL_RUBBER',
    # 0x142: 'DIGI_TOOL_BRUSH',
    # 0x143: 'DIGI_TOOL_PENCIL',
    # 0x144: 'DIGI_TOOL_AIRBRUSH',
    # 0x145: 'DIGI_TOOL_FINGER',
    # 0x146: 'DIGI_TOOL_MOUSE',
    # 0x147: 'DIGI_TOOL_LENS',
    # 0x148: 'DIGI_TOOL_QUINTTAP',
    # 0x149: 'DIGI_STYLUS3',
    # 0x14a: 'DIGI_TOUCH',
    # 0x14b: 'DIGI_STYLUS',
    # 0x14c: 'DIGI_STYLUS2',
    # 0x14d: 'DIGI_TOOL_DOUBLETAP',
    # 0x14e: 'DIGI_TOOL_TRIPLETAP',
    # 0x14f: 'DIGI_TOOL_QUADTAP',
    # 0x150: 'WHEEL_GEAR_DOWN',
    # 0x151: 'WHEEL_GEAR_UP',
}


def translate_keycode(code, ctrl=0, shift=0, alt=0, meta=0):
    try:
        key = keys[code]
        key = (
            f"{'CTRL+' if ctrl else ''}{'ALT+' if alt else ''}{'SHIFT+' if shift else ''}"
            f"{'META+' if meta else ''}{key}"
        )
        return key
    except KeyError:
        return None

# https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h # LEFT # LEFT # RIGHT # LEFT # Numpad # RIGHT # RIGHT # LEFT # RIGHT # 0x110: 'MOUSE_LEFT', # 0x111: 'MOUSE_RIGHT', # 0x112: 'MOUSE_MIDDLE', # 0x113: 'MOUSE_SIDE', # 0x114: 'MOUSE_EXTRA', # 0x115: 'MOUSE_FORWARD', # 0x116: 'MOUSE_BACK', # 0x117: 'MOUSE_TASK', # 0x120: 'JOYSTICK_TRIGGER', # 0x121: 'JOYSTICK_THUMB', # 0x122: 'JOYSTICK_THUMB2', # 0x123: 'JOYSTICK_TOP', # 0x124: 'JOYSTICK_TOP2', # 0x125: 'JOYSTICK_PINKIE', # 0x126: 'JOYSTICK_BASE', # 0x127: 'JOYSTICK_BASE2', # 0x128: 'JOYSTICK_BASE3', # 0x129: 'JOYSTICK_BASE4', # 0x12a: 'JOYSTICK_BASE5', # 0x12b: 'JOYSTICK_BASE6', # 0x12f: 'JOYSTICK_DEAD', # # 0x130: 'GAMEPAD_SOUTH', # 0x131: 'GAMEPAD_EAST', # 0x132: 'GAMEPAD_C', # 0x133: 'GAMEPAD_NORTH', # 0x134: 'GAMEPAD_WEST', # 0x135: 'GAMEPAD_Z', # 0x136: 'GAMEPAD_TL', # 0x137: 'GAMEPAD_TR', # 0x138: 'GAMEPAD_TL2', # 0x139: 'GAMEPAD_TR2', # 0x13a: 'GAMEPAD_SELECT', # 0x13b: 'GAMEPAD_START', # 0x13c: 'GAMEPAD_MODE', # 0x13d: 'GAMEPAD_THUMBL', # 0x13e: 'GAMEPAD_THUMBR', # # 0x140: 'DIGI_TOOL_PEN', # 0x141: 'DIGI_TOOL_RUBBER', # 0x142: 'DIGI_TOOL_BRUSH', # 0x143: 'DIGI_TOOL_PENCIL', # 0x144: 'DIGI_TOOL_AIRBRUSH', # 0x145: 'DIGI_TOOL_FINGER', # 0x146: 'DIGI_TOOL_MOUSE', # 0x147: 'DIGI_TOOL_LENS', # 0x148: 'DIGI_TOOL_QUINTTAP', # 0x149: 'DIGI_STYLUS3', # 0x14a: 'DIGI_TOUCH', # 0x14b: 'DIGI_STYLUS', # 0x14c: 'DIGI_STYLUS2', # 0x14d: 'DIGI_TOOL_DOUBLETAP', # 0x14e: 'DIGI_TOOL_TRIPLETAP', # 0x14f: 'DIGI_TOOL_QUADTAP', # 0x150: 'WHEEL_GEAR_DOWN', # 0x151: 'WHEEL_GEAR_UP',