From 5012611bd72bc74cef2a4870b9056df406b5dccb Mon Sep 17 00:00:00 2001 From: mathieui Date: Thu, 24 Apr 2014 21:13:31 +0200 Subject: [PATCH] Make keyboard.py a bit nicer to use --- src/keyboard.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/keyboard.py b/src/keyboard.py index 94eba47f..0a1391ea 100755 --- a/src/keyboard.py +++ b/src/keyboard.py @@ -179,13 +179,36 @@ class Keyboard(object): return ret_list if __name__ == '__main__': + import sys keyboard = Keyboard() s = curses.initscr() curses.noecho() curses.cbreak() s.keypad(1) + curses.start_color() + curses.use_default_colors() + curses.init_pair(1, 2, -1) + s.attron(curses.A_BOLD | curses.color_pair(1)) + s.addstr('Type Ctrl-c to close\n') + s.attroff(curses.A_BOLD | curses.color_pair(1)) + pressed_chars = [] while True: - chars = keyboard.get_user_input(s) - for char in chars if chars else '': - s.addstr('%s ' % (char)) + + try: + chars = keyboard.get_user_input(s) + for char in chars if chars else '': + s.addstr('%s ' % (char)) + pressed_chars.append(chars) + + except KeyboardInterrupt: + break + curses.echo() + curses.cbreak() + curses.curs_set(1) + curses.endwin() + for char_list in pressed_chars: + if char_list: + print(' '.join((char for char in char_list)), end=' ') + print() + sys.exit(0)