Created
September 9, 2022 14:42
-
-
Save zadjii-msft/661e4d64ffd1d19e11c63367ed94f0e3 to your computer and use it in GitHub Desktop.
Used for decoding win32 input records
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
vkey_table = { | |
0x01: "VK_LBUTTON", # "Left mouse button" | |
0x02: "VK_RBUTTON", # "Right mouse button" | |
0x03: "VK_CANCEL", # "Control-break processing" | |
0x04: "VK_MBUTTON", # "Middle mouse button (three-button mouse)" | |
0x05: "VK_XBUTTON1", # "X1 mouse button" | |
0x06: "VK_XBUTTON2", # "X2 mouse button" | |
0x07: "Undefined", # "Undefined" | |
0x08: "VK_BACK", # "BACKSPACE key" | |
0x09: "VK_TAB", # "TAB key" | |
0x0A: "Reserved", # "Reserved" | |
0x0B: "Reserved", # "Reserved" | |
0x0C: "VK_CLEAR", # "CLEAR key" | |
0x0D: "VK_RETURN", # "ENTER key" | |
0x0E: "Undefined", # "Undefined" | |
0x0F: "Undefined", # "Undefined" | |
0x10: "VK_SHIFT", # "SHIFT key" | |
0x11: "VK_CONTROL", # "CTRL key" | |
0x12: "VK_MENU", # "ALT key" | |
0x13: "VK_PAUSE", # "PAUSE key" | |
0x14: "VK_CAPITAL", # "CAPS LOCK key" | |
0x15: "VK_KANA", # "IME Kana mode" | |
0x15: "VK_HANGUEL", # "IME Hanguel mode (maintained for compatibility; use `VK_HANGUL`)" | |
0x15: "VK_HANGUL", # "IME Hangul mode" | |
0x16: "VK_IME_ON", # "IME On" | |
0x17: "VK_JUNJA", # "IME Junja mode" | |
0x18: "VK_FINAL", # "IME final mode" | |
0x19: "VK_HANJA", # "IME Hanja mode" | |
0x19: "VK_KANJI", # "IME Kanji mode" | |
0x1A: "VK_IME_OFF", # "IME Off" | |
0x1B: "VK_ESCAPE", # "ESC key" | |
0x1C: "VK_CONVERT", # "IME convert" | |
0x1D: "VK_NONCONVERT", # "IME nonconvert" | |
0x1E: "VK_ACCEPT", # "IME accept" | |
0x1F: "VK_MODECHANGE", # "IME mode change request" | |
0x20: "VK_SPACE", # "SPACEBAR" | |
0x21: "VK_PRIOR", # "PAGE UP key" | |
0x22: "VK_NEXT", # "PAGE DOWN key" | |
0x23: "VK_END", # "END key" | |
0x24: "VK_HOME", # "HOME key" | |
0x25: "VK_LEFT", # "LEFT ARROW key" | |
0x26: "VK_UP", # "UP ARROW key" | |
0x27: "VK_RIGHT", # "RIGHT ARROW key" | |
0x28: "VK_DOWN", # "DOWN ARROW key" | |
0x29: "VK_SELECT", # "SELECT key" | |
0x2A: "VK_PRINT", # "PRINT key" | |
0x2B: "VK_EXECUTE", # "EXECUTE key" | |
0x2C: "VK_SNAPSHOT", # "PRINT SCREEN key" | |
0x2D: "VK_INSERT", # "INS key" | |
0x2E: "VK_DELETE", # "DEL key" | |
0x2F: "VK_HELP", # "HELP key" | |
0x30: "0 key", # "0 key" | |
0x31: "1 key", # "1 key" | |
0x32: "2 key", # "2 key" | |
0x33: "3 key", # "3 key" | |
0x34: "4 key", # "4 key" | |
0x35: "5 key", # "5 key" | |
0x36: "6 key", # "6 key" | |
0x37: "7 key", # "7 key" | |
0x38: "8 key", # "8 key" | |
0x39: "9 key", # "9 key" | |
0x3A: "Undefined", # "Undefined" | |
0x3B: "Undefined", # "Undefined" | |
0x3C: "Undefined", # "Undefined" | |
0x3D: "Undefined", # "Undefined" | |
0x3E: "Undefined", # "Undefined" | |
0x3F: "Undefined", # "Undefined" | |
0x40: "Undefined", # "Undefined" | |
0x41: "A key", # "A key" | |
0x42: "B key", # "B key" | |
0x43: "C key", # "C key" | |
0x44: "D key", # "D key" | |
0x45: "E key", # "E key" | |
0x46: "F key", # "F key" | |
0x47: "G key", # "G key" | |
0x48: "H key", # "H key" | |
0x49: "I key", # "I key" | |
0x4A: "J key", # "J key" | |
0x4B: "K key", # "K key" | |
0x4C: "L key", # "L key" | |
0x4D: "M key", # "M key" | |
0x4E: "N key", # "N key" | |
0x4F: "O key", # "O key" | |
0x50: "P key", # "P key" | |
0x51: "Q key", # "Q key" | |
0x52: "R key", # "R key" | |
0x53: "S key", # "S key" | |
0x54: "T key", # "T key" | |
0x55: "U key", # "U key" | |
0x56: "V key", # "V key" | |
0x57: "W key", # "W key" | |
0x58: "X key", # "X key" | |
0x59: "Y key", # "Y key" | |
0x5A: "Z key", # "Z key" | |
0x5B: "VK_LWIN", # "Left Windows key (Natural keyboard)" | |
0x5C: "VK_RWIN", # "Right Windows key (Natural keyboard)" | |
0x5D: "VK_APPS", # "Applications key (Natural keyboard)" | |
0x5E: "-", # "Reserved" | |
0x5F: "VK_SLEEP", # "Computer Sleep key" | |
0x60: "VK_NUMPAD0", # "Numeric keypad 0 key" | |
0x61: "VK_NUMPAD1", # "Numeric keypad 1 key" | |
0x62: "VK_NUMPAD2", # "Numeric keypad 2 key" | |
0x63: "VK_NUMPAD3", # "Numeric keypad 3 key" | |
0x64: "VK_NUMPAD4", # "Numeric keypad 4 key" | |
0x65: "VK_NUMPAD5", # "Numeric keypad 5 key" | |
0x66: "VK_NUMPAD6", # "Numeric keypad 6 key" | |
0x67: "VK_NUMPAD7", # "Numeric keypad 7 key" | |
0x68: "VK_NUMPAD8", # "Numeric keypad 8 key" | |
0x69: "VK_NUMPAD9", # "Numeric keypad 9 key" | |
0x6A: "VK_MULTIPLY", # "Multiply key" | |
0x6B: "VK_ADD", # "Add key" | |
0x6C: "VK_SEPARATOR", # "Separator key" | |
0x6D: "VK_SUBTRACT", # "Subtract key" | |
0x6E: "VK_DECIMAL", # "Decimal key" | |
0x6F: "VK_DIVIDE", # "Divide key" | |
0x70: "VK_F1", # "F1 key" | |
0x71: "VK_F2", # "F2 key" | |
0x72: "VK_F3", # "F3 key" | |
0x73: "VK_F4", # "F4 key" | |
0x74: "VK_F5", # "F5 key" | |
0x75: "VK_F6", # "F6 key" | |
0x76: "VK_F7", # "F7 key" | |
0x77: "VK_F8", # "F8 key" | |
0x78: "VK_F9", # "F9 key" | |
0x79: "VK_F10", # "F10 key" | |
0x7A: "VK_F11", # "F11 key" | |
0x7B: "VK_F12", # "F12 key" | |
0x7C: "VK_F13", # "F13 key" | |
0x7D: "VK_F14", # "F14 key" | |
0x7E: "VK_F15", # "F15 key" | |
0x7F: "VK_F16", # "F16 key" | |
0x80: "VK_F17", # "F17 key" | |
0x81: "VK_F18", # "F18 key" | |
0x82: "VK_F19", # "F19 key" | |
0x83: "VK_F20", # "F20 key" | |
0x84: "VK_F21", # "F21 key" | |
0x85: "VK_F22", # "F22 key" | |
0x86: "VK_F23", # "F23 key" | |
0x87: "VK_F24", # "F24 key" | |
0x88: "Unassigned", # "Unassigned" | |
0x89: "Unassigned", # "Unassigned" | |
0x8A: "Unassigned", # "Unassigned" | |
0x8B: "Unassigned", # "Unassigned" | |
0x8C: "Unassigned", # "Unassigned" | |
0x8D: "Unassigned", # "Unassigned" | |
0x8E: "Unassigned", # "Unassigned" | |
0x8F: "Unassigned", # "Unassigned" | |
0x90: "VK_NUMLOCK", # "NUM LOCK key" | |
0x91: "VK_SCROLL", # "SCROLL LOCK key" | |
0x97: "Unassigned", # "Unassigned" | |
0x98: "Unassigned", # "Unassigned" | |
0x99: "Unassigned", # "Unassigned" | |
0x9A: "Unassigned", # "Unassigned" | |
0x9B: "Unassigned", # "Unassigned" | |
0x9C: "Unassigned", # "Unassigned" | |
0x9D: "Unassigned", # "Unassigned" | |
0x9E: "Unassigned", # "Unassigned" | |
0x9F: "Unassigned", # "Unassigned" | |
0xA0: "VK_LSHIFT", # "Left SHIFT key" | |
0xA1: "VK_RSHIFT", # "Right SHIFT key" | |
0xA2: "VK_LCONTROL", # "Left CONTROL key" | |
0xA3: "VK_RCONTROL", # "Right CONTROL key" | |
0xA4: "VK_LMENU", # "Left ALT key" | |
0xA5: "VK_RMENU", # "Right ALT key" | |
0xA6: "VK_BROWSER_BACK", # "Browser Back key" | |
0xA7: "VK_BROWSER_FORWARD", # "Browser Forward key" | |
0xA8: "VK_BROWSER_REFRESH", # "Browser Refresh key" | |
0xA9: "VK_BROWSER_STOP", # "Browser Stop key" | |
0xAA: "VK_BROWSER_SEARCH", # "Browser Search key" | |
0xAB: "VK_BROWSER_FAVORITES", # "Browser Favorites key" | |
0xAC: "VK_BROWSER_HOME", # "Browser Start and Home key" | |
0xAD: "VK_VOLUME_MUTE", # "Volume Mute key" | |
0xAE: "VK_VOLUME_DOWN", # "Volume Down key" | |
0xAF: "VK_VOLUME_UP", # "Volume Up key" | |
0xB0: "VK_MEDIA_NEXT_TRACK", # "Next Track key" | |
0xB1: "VK_MEDIA_PREV_TRACK", # "Previous Track key" | |
0xB2: "VK_MEDIA_STOP", # "Stop Media key" | |
0xB3: "VK_MEDIA_PLAY_PAUSE", # "Play/Pause Media key" | |
0xB4: "VK_LAUNCH_MAIL", # "Start Mail key" | |
0xB5: "VK_LAUNCH_MEDIA_SELECT", # "Select Media key" | |
0xB6: "VK_LAUNCH_APP1", # "Start Application 1 key" | |
0xB7: "VK_LAUNCH_APP2", # "Start Application 2 key" | |
0xBA: "VK_OEM_1", # "Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ';:' key" | |
0xBB: "VK_OEM_PLUS", # "For any country/region, the '+' key" | |
0xBC: "VK_OEM_COMMA", # "For any country/region, the ',' key" | |
0xBD: "VK_OEM_MINUS", # "For any country/region, the '-' key" | |
0xBE: "VK_OEM_PERIOD", # "For any country/region, the '.' key" | |
0xBF: "VK_OEM_2", # "Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '/?' key" | |
0xC0: "VK_OEM_3", # "Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '`~' key" | |
0xDB: "VK_OEM_4", # "Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '[{' key" | |
0xDC: "VK_OEM_5", # "Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the '\\|' key" | |
0xDD: "VK_OEM_6", # "Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the ']}' key" | |
0xDE: "VK_OEM_7", # "Used for miscellaneous characters; it can vary by keyboard. For the US standard keyboard, the 'single-quote/double-quote' key" | |
0xDF: "VK_OEM_8", # "Used for miscellaneous characters; it can vary by keyboard." | |
0xE0: "-", # "Reserved" | |
0xE1: "OEM specific", # "OEM specific" | |
0xE2: "VK_OEM_102", # "The <> keys on the US standard keyboard, or the \|key on the non-US 102-key keyboard" | |
0xE5: "VK_PROCESSKEY", # "IME PROCESS key" | |
0xE6: "OEM specific", # "OEM specific" | |
0xE7: "VK_PACKET", # "Used to pass Unicode characters as if they were keystrokes. The `VK_PACKET` key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, "see Remark in `KEYBDINPUT`, `SendInput`,`WM_KEYDOWN`, and `WM_KEYUP` | |
0xE8: "-", # "Unassigned" | |
0xF6: "VK_ATTN", # "Attn key" | |
0xF7: "VK_CRSEL", # "CrSel key" | |
0xF8: "VK_EXSEL", # "ExSel key" | |
0xF9: "VK_EREOF", # "Erase EOF key" | |
0xFA: "VK_PLAY", # "Play key" | |
0xFB: "VK_ZOOM", # "Zoom key" | |
0xFC: "VK_NONAME", # "Reserved" | |
0xFD: "VK_PA1", # "PA1 key" | |
0xFE: "VK_OEM_CLEAR", # "Clear key" | |
} | |
def main(): | |
for line in sys.stdin: | |
# print(line) | |
if line == "\x03" or line == "\x04": | |
return | |
if len(line) <= 0: | |
continue | |
print(f"* `{line[:-1]}`") | |
if not line[0] == "␛": | |
continue | |
# Sequences are formatted as follows: | |
# | |
# ^[ [ Vk ; Sc ; Uc ; Kd ; Cs ; Rc _ | |
# | |
# Vk: the value of wVirtualKeyCode - any number. If omitted, defaults to '0'. | |
# Sc: the value of wVirtualScanCode - any number. If omitted, defaults to '0'. | |
# Uc: the decimal value of UnicodeChar - for example, NUL is "0", LF is | |
# "10", the character 'A' is "65". If omitted, defaults to '0'. | |
# Kd: the value of bKeyDown - either a '0' or '1'. If omitted, defaults to '0'. | |
# Cs: the value of dwControlKeyState - any number. If omitted, defaults to '0'. | |
# Rc: the value of wRepeatCount - any number. If omitted, defaults to '1'. | |
parts = line[2:-2].split(";") | |
down = "down" if parts[3] == "1" else "up" | |
vkey = vkey_table[int(parts[0])] | |
char_value = int(parts[2]) | |
unicode_char = f"'{chr(char_value)}'" if char_value >= 0x20 else char_value | |
print(f" * `Vk`: {vkey}") | |
print(f" * `Sc`: {parts[1]}") | |
print(f" * `Uc`: {unicode_char}") | |
print(f" * `Kd`: {down}`") | |
print(f" * `Cs`: {parts[4]}") | |
# print(f" * `Rc:{parts[5]}`") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment