#!/usr/bin/env python # -*- coding: utf-8 -*- # test ncurses new [win.] get_wch method binding # it expects to be run on a UTF-8 terminal # run program and write "façade" and press enter, do it twice # note the differences import curses import locale locale.setlocale(locale.LC_ALL, '') g_encoding = locale.getpreferredencoding() def getcodes(win): win.addstr(u'Type "façade" and press enter\n'.encode(g_encoding)) codes = [] while True: ch = win.getch() # traditional 1 byte if ch == 10: # enter break codes.append(ch) win.addstr(u'One more time, type "façade" and press enter\n'.encode(g_encoding)) codes_w = [] while True: ch = win.get_wch() # new method if ch == 10: # enter break codes_w.append(ch) return codes, codes_w codes, codes_w = curses.wrapper(getcodes) print 'Terminal encoding:', g_encoding print 'With "getch" method: ', codes, ' => ', for c in codes: print unichr(c).encode('utf-8'), print print 'With new "get_wch" method: ', codes_w, ' => ', for c in codes_w: print unichr(c).encode('utf-8'), print