From 20e31b63ddbd1e59c79990a2a9834e8d217bfe3d Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Sun, 23 Nov 2008 11:23:57 +0100 Subject: [PATCH] Add list of keysyms We provide a module that can convert unicode points to descriptive keysym names. The list is currently built by reading the appropriate X11 include, rather than being built in the module itself. --- keysymdata.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 keysymdata.py diff --git a/keysymdata.py b/keysymdata.py new file mode 100755 index 0000000..b3bc47a --- /dev/null +++ b/keysymdata.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python + +from __future__ import with_statement + +import os +import re + +_re = re.compile('^#define XK_([a-zA-Z_0-9]+)\s+0x([0-9A-Fa-f]+)\s*/\*\s*U\+([0-9A-Fa-f]{4,6}) (.*)\s+\*/\s*$') + +# TODO provide defaults from current X.org +_uni2name = {} +_name2uni = {} +_keysym = {} + +_hfile = os.path.join('X11', 'keysymdef.h') + +_include = None + +# TODO include dirs: should be portable +_candidates = ['/usr/local/include', '/usr/include', '/usr/share/include'] +for dir in _candidates: + _include = os.path.join(dir, _hfile) + if os.path.exists(_include): + break + +if _include is not None: + try: + with open(_include, 'r') as file: + for line in file: + match = _re.match(line.strip()) + if match: + name = match.group(1) + code = match.group(2) + uni = match.group(3).lower() + uniname = match.group(4) + _uni2name[uni] = name # FIXME what to do with duplicates? + _name2uni[name] = uni + _keysym[name] = code + except IOError: + pass + +def name(uni): + if uni is None: + return "NoSymbol" + key = None + if isinstance(uni, int): + key = "%04x" % uni + elif isinstance(uni, basestring): + if len(uni) == 1: + key = "%04x" % ord(uni) + elif len(uni) == 4: + key = uni + if key is None: + raise ValueError + if key in _uni2name: + return _uni2name[key] + return "U%s" % key.upper() + +if __name__ == '__main__': + for u in range(0,0xffff): + print "U+%04X\t%s" % tuple([u, name(u)]) + -- 2.32.0.93.g670b81a890