Skip to content
Advertisement

How to capture escape sequences sent by terminal?

How would one capture the escape sequences as they are sent by a terminal application (say Konsole for example) ? For example, if you hit PgDown, what is sent to the virtual console ?

I would like to record the byte stream sent to the virtual console (like when I hit “Ctrl+C” what escape sequence it produced) to a file I can then read with hexdump.

Advertisement

Answer

I did a small python script to do the trick :

#!/bin/env python

import curses
from pprint import pprint

buf = ''

def main(stdscr):
  global buf
  curses.noecho()
  curses.raw()
  curses.cbreak()
  stdscr.keypad(False)

  stop = stdscr.getkey()
  c = stdscr.getkey()
  buf = ''
  while c != stop:
    buf += c
    c = stdscr.getkey()

def run():
  curses.wrapper(main)
  pprint(buf)
  tmp = buf.encode('latin1')
  pprint([hex(x) for x in tmp])
  pprint([bin(x) for x in tmp])

run()

…It clear the screen, then type a key (e.g. a), then, type any thing, and type the same key as the first one to stop. Then, it will display all the bytes received (example : a [start recording] Alt+b [stop recording] a produces the bytes : ['0x1b', '0x62'] with my terminal

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement