diff options
author | louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13> | 2010-09-03 18:01:43 +0000 |
---|---|---|
committer | louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13> | 2010-09-03 18:01:43 +0000 |
commit | 17eb99eea718191b619277754bf15e6a7554d916 (patch) | |
tree | aa39d3583025b4a49f7492245981cd519c6855e9 /src/keyboard.py | |
parent | 43654505fb2efa3994081e68613e5bd8dd033660 (diff) | |
download | poezio-17eb99eea718191b619277754bf15e6a7554d916.tar.gz poezio-17eb99eea718191b619277754bf15e6a7554d916.tar.bz2 poezio-17eb99eea718191b619277754bf15e6a7554d916.tar.xz poezio-17eb99eea718191b619277754bf15e6a7554d916.zip |
python 3 only. Fixes all EncodingError bugs :))))
Diffstat (limited to 'src/keyboard.py')
-rw-r--r-- | src/keyboard.py | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/keyboard.py b/src/keyboard.py index 9fdef730..06bad196 100644 --- a/src/keyboard.py +++ b/src/keyboard.py @@ -1,5 +1,3 @@ -# -*- coding:utf-8 -*- -# # Copyright 2010 Le Coz Florent <louizatakk@fedoraproject.org> # # This file is part of Poezio. @@ -26,6 +24,9 @@ shortcut, like ^A, M-a or KEY_RESIZE) def get_next_byte(s): """ Read the next byte of the utf-8 char + ncurses seems to return a string of the byte + encoded in latin-1. So what we get is NOT what we typed + unless we do the conversion… """ try: c = s.getkey() @@ -33,7 +34,7 @@ def get_next_byte(s): return (None, "KEY_RESIZE") if len(c) >= 4: return (None, c) - return (ord(c), c) + return (ord(c), c.encode('latin-1')) # returns a number and a bytes object def read_char(s): """ @@ -47,10 +48,10 @@ def read_char(s): return "KEY_BACKSPACE" if first < 127: # ASCII char on one byte if first <= 26: # transform Ctrl+* keys - char = "^"+chr(first + 64) + return "^"+chr(first + 64) if first == 27: (first, c) = get_next_byte(s) - char = "M-"+c + return "M-"+c if 194 <= first: (code, c) = get_next_byte(s) # 2 bytes char char += c @@ -60,4 +61,11 @@ def read_char(s): if 240 <= first: (code, c) = get_next_byte(s) # 4 bytes char char += c - return char + return char.decode('utf-8')# return all the concatened byte objets, decoded + +if __name__ == '__main__': + import curses + s = curses.initscr() + curses.noecho() + while True: + s.addstr('%s\n' % read_char(s)) |