summaryrefslogtreecommitdiff
path: root/src/keyboard.py
diff options
context:
space:
mode:
authorlouiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13>2010-07-08 19:28:40 +0000
committerlouiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13>2010-07-08 19:28:40 +0000
commitf30040a88c771f39a6cb2cf2cdbf0a9ca93e0eb6 (patch)
tree282986d727774220af42eb08e0b5f8de3187484b /src/keyboard.py
parent69c23047a9a70ca00b1f04b4112ab6f9debf329b (diff)
downloadpoezio-f30040a88c771f39a6cb2cf2cdbf0a9ca93e0eb6.tar.gz
poezio-f30040a88c771f39a6cb2cf2cdbf0a9ca93e0eb6.tar.bz2
poezio-f30040a88c771f39a6cb2cf2cdbf0a9ca93e0eb6.tar.xz
poezio-f30040a88c771f39a6cb2cf2cdbf0a9ca93e0eb6.zip
Also add this file. Handle properly all utf-8 char entered, and all keyboard shortcuts
Diffstat (limited to 'src/keyboard.py')
-rw-r--r--src/keyboard.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/keyboard.py b/src/keyboard.py
new file mode 100644
index 00000000..c9836250
--- /dev/null
+++ b/src/keyboard.py
@@ -0,0 +1,60 @@
+#!/usr/bin/python
+# -*- coding:utf-8 -*-
+#
+# Copyright 2010 Le Coz Florent <louizatakk@fedoraproject.org>
+#
+# 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
+shortcut, like ^A or KEY_RESIZE)
+"""
+
+def get_next_byte(s):
+ """
+ Read the next byte of the utf-8 char
+ """
+ c = s.getkey()
+ if len(c) > 4:
+ return (None, c)
+ return (ord(c), c)
+
+def read_char(s):
+ """
+ Read one utf-8 char
+ see http://en.wikipedia.org/wiki/UTF-8#Description
+ """
+ (first, char) = get_next_byte(s)
+ if first == None: # Keyboard special, like KEY_HOME etc
+ return char
+ if first <= 127: # ASCII char on one byte
+ if first <= 26: # transform Ctrl+* keys
+ char = "^"+chr(first + 64)
+ if first == 27:
+ (_, c) = get_next_byte(s)
+ char = "M-"+c
+ return char
+ 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
+ if 240 <= code:
+ (code, c) = get_next_byte(s) # 4 bytes char
+ char += c
+ return char