Skip to content

Instantly share code, notes, and snippets.

@parke
Last active January 1, 2016 19:19
Show Gist options
  • Save parke/8189675 to your computer and use it in GitHub Desktop.
Save parke/8189675 to your computer and use it in GitHub Desktop.
curev.py is a curses event monitor/dumper. It is similar to xev for X11/Xwindows, except it runs in a terminal. I use curev.py to examine what escape sequences various keys generate across various terminals.
import collections, curses, threading
event = threading.Event ()
keys = collections.deque ()
run = threading.Event ()
def reduce ( keys ):
t = []
for n in keys :
if n == 0 : t.append ( '^@' )
elif n <= 26 : t.append ( '^' + chr ( ord ( 'a' ) + n - 1 ) )
elif n == 27 : t.append ( '\e' )
elif n >= 33 and n <= 126 : t.append ( chr ( n ) )
else: t.append ( str ( n ) )
return ''.join (t)
def flush ( stdscr ):
while run.is_set ():
event.clear ()
if not event.wait ( 0.02 ) and keys :
stdscr.addstr ( '%20s ' % reduce ( keys ) )
for n in keys :
stdscr.addstr ( ' %3d' % n )
stdscr.addstr ( '\n' )
stdscr.refresh ()
keys.clear ()
def main ():
stdscr = curses.initscr ()
curses.noecho ()
curses.cbreak ()
stdscr.keypad ( True )
stdscr.scrollok ( True )
stdscr.clear ()
stdscr.refresh ()
run.set ()
thread = threading.Thread ( target = flush, args = (stdscr,) )
thread.start ()
try:
f = open ('/dev/tty', 'rb')
while True :
c = f.read (1)
if not c : break
keys.append (c[0])
event.set ()
except: pass
run.clear ()
thread.join ()
curses.nocbreak ()
stdscr.keypad ( False )
curses.echo ()
curses.endwin ()
print ('curev done')
main ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment