| 1 | #!/usr/bin/env python3 |
| 2 | """Probe the kitty keyboard protocol by printing the raw bytes each key sends. |
| 3 | |
| 4 | Run it inside a terminal to see what that terminal emits under a chosen set of |
| 5 | progressive-enhancement flags: |
| 6 | |
| 7 | python3 scripts/kitty-probe.py [flags] |
| 8 | |
| 9 | `flags` is the decimal flag set to enable (default 1): |
| 10 | |
| 11 | 1 disambiguate escape codes |
| 12 | 2 report event types (press/repeat/release) |
| 13 | 4 report alternate keys |
| 14 | 8 report all keys as escape codes |
| 15 | 16 report associated text |
| 16 | |
| 17 | Combine by adding, e.g. 15 for everything but text, 31 for everything. Press |
| 18 | Esc to quit; the previous keyboard mode is restored on exit. |
| 19 | """ |
| 20 | |
| 21 | import os |
| 22 | import sys |
| 23 | import termios |
| 24 | import tty |
| 25 | |
| 26 | flags = int(sys.argv[1]) if len(sys.argv) > 1 else 1 |
| 27 | fd = sys.stdin.fileno() |
| 28 | old = termios.tcgetattr(fd) |
| 29 | try: |
| 30 | tty.setraw(fd) |
| 31 | os.write(1, f"\033[>{flags}u".encode()) # push current flags and enable |
| 32 | os.write(1, f"flags={flags}; keys print as raw bytes, Esc quits\r\n".encode()) |
| 33 | while True: |
| 34 | b = os.read(fd, 64) |
| 35 | os.write(1, (repr(b) + "\r\n").encode()) |
| 36 | if b == b"\x1b" or b"27u" in b: # Esc in legacy or CSI u form |
| 37 | break |
| 38 | finally: |
| 39 | os.write(1, b"\033[<u") # pop back to the previous mode |
| 40 | termios.tcsetattr(fd, termios.TCSADRAIN, old) |