2011-01-01 13:27:07 +00:00
|
|
|
# Copyright 2010-2011 Le Coz Florent <louiz@louiz.org>
|
2010-07-08 19:28:40 +00:00
|
|
|
#
|
|
|
|
# This file is part of Poezio.
|
|
|
|
#
|
|
|
|
# Poezio is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Poezio is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Poezio. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Functions to interact with the keyboard
|
|
|
|
Mainly, read keys entered and return a string (most
|
|
|
|
of the time ONE char, but may be longer if it's a keyboard
|
2010-07-19 19:38:33 +00:00
|
|
|
shortcut, like ^A, M-a or KEY_RESIZE)
|
2010-07-08 19:28:40 +00:00
|
|
|
"""
|
|
|
|
|
2011-02-15 20:03:24 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
last_char_time = None
|
|
|
|
|
2010-07-08 19:28:40 +00:00
|
|
|
def get_next_byte(s):
|
|
|
|
"""
|
|
|
|
Read the next byte of the utf-8 char
|
2010-09-03 18:01:43 +00:00
|
|
|
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…
|
2010-07-08 19:28:40 +00:00
|
|
|
"""
|
2010-07-20 19:15:15 +00:00
|
|
|
try:
|
|
|
|
c = s.getkey()
|
|
|
|
except:
|
2010-07-20 19:34:06 +00:00
|
|
|
return (None, "KEY_RESIZE")
|
2010-08-07 00:28:47 +00:00
|
|
|
if len(c) >= 4:
|
2010-07-08 19:28:40 +00:00
|
|
|
return (None, c)
|
2010-09-03 18:01:43 +00:00
|
|
|
return (ord(c), c.encode('latin-1')) # returns a number and a bytes object
|
2010-07-08 19:28:40 +00:00
|
|
|
|
|
|
|
def read_char(s):
|
|
|
|
"""
|
|
|
|
Read one utf-8 char
|
|
|
|
see http://en.wikipedia.org/wiki/UTF-8#Description
|
|
|
|
"""
|
2011-02-15 20:03:24 +00:00
|
|
|
# We use a timer to know if we are pasting from the
|
|
|
|
# clipboard or not
|
|
|
|
global last_char_time
|
|
|
|
last_char_time = time.time()
|
2010-07-08 19:28:40 +00:00
|
|
|
(first, char) = get_next_byte(s)
|
2010-09-08 23:00:55 +00:00
|
|
|
if not isinstance(first, int): # Keyboard special, like KEY_HOME etc
|
2010-07-08 19:28:40 +00:00
|
|
|
return char
|
2010-07-20 21:57:25 +00:00
|
|
|
if first == 127 or first == 8:
|
|
|
|
return "KEY_BACKSPACE"
|
|
|
|
if first < 127: # ASCII char on one byte
|
2010-07-08 19:28:40 +00:00
|
|
|
if first <= 26: # transform Ctrl+* keys
|
2011-02-15 20:03:24 +00:00
|
|
|
char = chr(first + 64)
|
2011-02-15 21:16:45 +00:00
|
|
|
if char == 'M' and time.time() - last_char_time < 0.0005:
|
2011-02-15 20:03:24 +00:00
|
|
|
char = 'J'
|
|
|
|
return "^"+char
|
2010-07-08 19:28:40 +00:00
|
|
|
if first == 27:
|
2011-02-15 19:25:32 +00:00
|
|
|
second = read_char(s)
|
|
|
|
res = 'M-%s' % (second,)
|
|
|
|
if second == '[':
|
|
|
|
for i in range(4):
|
|
|
|
res += read_char(s)
|
|
|
|
return res
|
2010-07-08 19:28:40 +00:00
|
|
|
if 194 <= first:
|
|
|
|
(code, c) = get_next_byte(s) # 2 bytes char
|
|
|
|
char += c
|
|
|
|
if 224 <= first:
|
|
|
|
(code, c) = get_next_byte(s) # 3 bytes char
|
|
|
|
char += c
|
2010-07-19 19:38:33 +00:00
|
|
|
if 240 <= first:
|
2010-07-08 19:28:40 +00:00
|
|
|
(code, c) = get_next_byte(s) # 4 bytes char
|
|
|
|
char += c
|
2010-09-10 21:51:13 +00:00
|
|
|
try:
|
|
|
|
return char.decode('utf-8') # return all the concatened byte objets, decoded
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
return None
|
2010-09-03 18:01:43 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import curses
|
|
|
|
s = curses.initscr()
|
2011-02-15 19:25:32 +00:00
|
|
|
curses.curs_set(1)
|
|
|
|
curses.noecho()
|
|
|
|
curses.nonl()
|
|
|
|
s.keypad(True)
|
2010-09-03 18:01:43 +00:00
|
|
|
curses.noecho()
|
|
|
|
while True:
|
|
|
|
s.addstr('%s\n' % read_char(s))
|