Skip to content

Instantly share code, notes, and snippets.

@mcprat
Last active October 10, 2022 23:17
Show Gist options
  • Save mcprat/e732c474205b317af053a9b14df211bc to your computer and use it in GitHub Desktop.
Save mcprat/e732c474205b317af053a9b14df211bc to your computer and use it in GitHub Desktop.
msvcrt.getch() function key scan codes to ANSI escape code dictionary
# SPDX-License-Identifier: (GPL-2.0-only OR Apache-2.0)
"""
Windows function key scan codes to ANSI escape code dictionary:
Pause/Break, Ctrl+Alt+Del, Ctrl+Alt+arrows not mapable
Input: ordinal of char from msvcrt.getch()
Output: bytes string of ANSI escape sequence for linux/xterm
numerical used over linux specifics for Home and End
VT or CSI escape sequences used when linux has no sequence
something unique for keys without an escape function
\x1b == Escape key
"""
winfnkeys = {
#Ctrl + Alt + Backspace
14: b'\x1b^H',
#Ctrl + Alt + Enter
28: b'\x1b\r',
# Pause/Break
29: b'\x1c',
# Arrows
72: b'\x1b[A',
80: b'\x1b[B',
77: b'\x1b[C',
75: b'\x1b[D',
# Arrows (Alt)
152: b'\x1b[1;3A',
160: b'\x1b[1;3B',
157: b'\x1b[1;3C',
155: b'\x1b[1;3D',
# Arrows (Ctrl)
141: b'\x1b[1;5A',
145: b'\x1b[1;5B',
116: b'\x1b[1;5C',
115: b'\x1b[1;5D',
#Ctrl + Tab
148: b'\x1b[2J',
# Cursor (Home, Ins, Del...)
71: b'\x1b[1~',
82: b'\x1b[2~',
83: b'\x1b[3~',
79: b'\x1b[4~',
73: b'\x1b[5~',
81: b'\x1b[6~',
# Cursor + Alt
151: b'\x1b[1;3~',
162: b'\x1b[2;3~',
163: b'\x1b[3;3~',
159: b'\x1b[4;3~',
153: b'\x1b[5;3~',
161: b'\x1b[6;3~',
# Cursor + Ctrl (xterm)
119: b'\x1b[1;5H',
146: b'\x1b[2;5~',
147: b'\x1b[3;5~',
117: b'\x1b[1;5F',
134: b'\x1b[5;5~',
118: b'\x1b[6;5~',
# Function Keys (F1 - F12)
59: b'\x1b[11~',
60: b'\x1b[12~',
61: b'\x1b[13~',
62: b'\x1b[14~',
63: b'\x1b[15~',
64: b'\x1b[17~',
65: b'\x1b[18~',
66: b'\x1b[19~',
67: b'\x1b[20~',
68: b'\x1b[21~',
133: b'\x1b[23~',
134: b'\x1b[24~',
# Function Keys + Shift (F11 - F22)
84: b'\x1b[23;2~',
85: b'\x1b[24;2~',
86: b'\x1b[25~',
87: b'\x1b[26~',
88: b'\x1b[28~',
89: b'\x1b[29~',
90: b'\x1b[31~',
91: b'\x1b[32~',
92: b'\x1b[33~',
93: b'\x1b[34~',
135: b'\x1b[20;2~',
136: b'\x1b[21;2~',
# Function Keys + Ctrl (xterm)
94: b'\x1bOP',
95: b'\x1bOQ',
96: b'\x1bOR',
97: b'\x1bOS',
98: b'\x1b[15;2~',
99: b'\x1b[17;2~',
100: b'\x1b[18;2~',
101: b'\x1b[19;2~',
102: b'\x1b[20;3~',
103: b'\x1b[21;3~',
137: b'\x1b[23;3~',
138: b'\x1b[24;3~',
# Function Keys + Alt (xterm)
104: b'\x1b[11;5~',
105: b'\x1b[12;5~',
106: b'\x1b[13;5~',
107: b'\x1b[14;5~',
108: b'\x1b[15;5~',
109: b'\x1b[17;5~',
110: b'\x1b[18;5~',
111: b'\x1b[19;5~',
112: b'\x1b[20;5~',
113: b'\x1b[21;5~',
139: b'\x1b[23;5~',
140: b'\x1b[24;5~',
}
@inaky-intc
Copy link

@mpratt14: thanks for writing this, it is quite useful -- would like to use it in an Apache 2.0 Licensed codebase (https://github.com/intel/tcf); would you consider adding an Apache-2.0 compatible license for it?

@mcprat
Copy link
Author

mcprat commented Oct 10, 2022

you're welcome to take it, and list me for credit if possible

Signed-off-by: Michael Pratt <mcpratt@pm.me>

and you should know, I wrote this quickly and only tested on windows 10, on one machine, it's not perfect anyway

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment